How to get the Text of a Textbox inside a repeater? - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

How to get the Text of a Textbox inside a repeater?

Fred_silbFred_silb Posts: 3Member
Hello everyone,

Does anyone knows how to get the Text, that a user entered, of a Textbox inside a repeater? I'm not able to find a solution for this problem.

It seems that I can only retrieve the Text of controls with default Text and not with databound or Text that the user entered.

I'm able to retrieve the Text of a Textbox inside a repeater with a default value i.e.:

[code]
asd
[/code]

Then in codebehind I use this:

[code]
System.Web.UI.WebControls.TextBox MyText=Repeater1.Items[0].FindControl("TextBox1");
[/code]

That works well but with this code it doesn't work either:

[code]

<%# DataBinder.Eval(Container.DataItem,"Key")%>

[/code]

[code]
System.Web.UI.WebControls.Label MyLabel=Repeater1.Items[0].FindControl("Label1");
[/code]

I do get the control but with the Text empty =""

If anyone has any idea please let me know.

Here it is the html code:

[code]













<%# DataBinder.Eval(Container.DataItem,"Key")%>






[/code]

And here the codebehind:

[code]
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
if(IsPostBack)
{
}

else
{
MyHashTable=new Hashtable(2);
MyHashTable.Add("Hello",1);
MyHashTable.Add("Bye",2);

this.Repeater1.DataSource=MyHashTable;
this.Repeater1.DataBind();
}
}

private void Repeater1_ItemCommand(object source, System.Web.UI.WebControls.RepeaterCommandEventArgs e)
{
System.Web.UI.WebControls.TextBox MyTextBox=System.Web.UI.WebControls.TextBox)e.Item.FindControl("TextBox1");
System.Web.UI.WebControls.Label MyLabel=(System.Web.UI.WebControls.Label)e.Item.FindControl("Label1");
string mmmm=MyLabel.Text.ToString().TrimEnd();
mmmm=MyTextBox.Text.ToString().TrimEnd();
}

private void Repeater1_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
{
System.Web.UI.WebControls.TextBox MyTextBox=System.Web.UI.WebControls.TextBox)e.Item.FindControl("TextBox1");
System.Web.UI.WebControls.Label MyLabel=(System.Web.UI.WebControls.Label)e.Item.FindControl("Label1");
string mmmm=MyLabel.Text.ToString().TrimEnd();
mmmm=MyTextBox.Text.ToString().TrimEnd();
}
[/code]

Thanks a lot in advance

P.D: I don't want to assign a value to a textbox, I want an empty textbox so the user can enter something in there and then I want to retrieve what he wrote.

Comments

  • iwilld0itiwilld0it Posts: 1,134Member
    Here is the solution to your problem. For the label control change the databind expression like so ...

    From

    [code]

    <%# DataBinder.Eval(Container.DataItem,"Key")%>

    [/code]

    To

    [code]
    ' Runat="server" />
    [/code]

    [code]
    use System.Web.UI.WebControls;
    ...

    private void Repeater1_ItemCommand(object sender, RepeaterCommandEventArgs e)
    {
    TextBox MyTextBox = (TextBox)Repeater1.Item[0].FindControl("TextBox1");
    Label MyLabel(Label)Repeater1.Item[0].FindControl("Label1");

    Response.Write(MyTextBox.Text.Trim() + "
    ");
    Response.Write(MyLabel.Text.Trim() + "
    ");
    }
    [/code]

    Forgive me if my C# is a little off, considering i'm a VB person.

  • Fred_silbFred_silb Posts: 3Member
    Thanks for your time but what I'd like is to retrive(get) the Text that a user wrote in a Textbox that is inside a repeater.

    Thanks
  • Fred_silbFred_silb Posts: 3Member
    Ok, I went to barnes and nobles and I started reading some books until I found the solution to my problem in this book:

    Programming Microsoft ASP.Net
    ISBN 0-7356-1903-4
    by Dino Esposito

    Chapeter 9 is the one that talks about the repeater and datalists.

    Anyway here I'm posting the code so everyone that has the same problem can solve it:

    [code]












    <%# m_MyTextBox = (TextBox) FindTextBox(Container) %>





    Label

    [/code]

    If you check well only this needs to be added:

    [code]

    <%# m_MyTextBox = (TextBox) FindTextBox(Container) %>

    [/code]

    The codebehind code is like this:

    [code]
    protected System.Web.UI.WebControls.TextBox m_MyTextBox;

    private void Page_Load(object sender, System.EventArgs e)
    {
    // Put user code to initialize the page here
    if(IsPostBack)
    {
    }

    else
    {
    MyHashTable=new Hashtable(2);

    MyHashTable.Add("Hello",1);
    MyHashTable.Add("Bye",2);

    this.Repeater1.DataSource=MyHashTable;
    this.Repeater1.DataBind();
    }
    }

    #region Web Form Designer generated code
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN: This call is required by the ASP.NET Web Form Designer.
    //
    InitializeComponent();
    base.OnInit(e);
    }

    ///
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    ///
    private void InitializeComponent()
    {
    this.Load += new System.EventHandler(this.Page_Load);

    }
    #endregion

    protected System.Web.UI.WebControls.TextBox FindTextBox(System.Web.UI.Control container)
    {
    System.Web.UI.WebControls.TextBox MyT=(System.Web.UI.WebControls.TextBox)container.FindControl("TextBox1");

    return MyT;
    }

    protected void OnSubmit(object sender, EventArgs e)
    {
    TextBox MyT = (TextBox) Repeater1.Items[0].FindControl("TextBox1");
    Label1.Text=MyT.Text;
    }
    [/code]

    And here is the version without codebehind for the one that dont like it:
    [code]
    <%@ Page language="c#" %>


    WebForm4



    TextBox m_MyTextBox;

    public void Page_Load(object sender, EventArgs e)
    {
    if(IsPostBack)
    {
    }

    else
    {
    BindData();
    }
    }

    private void BindData()
    {
    Hashtable MyHashTable;

    MyHashTable=new Hashtable(2);

    MyHashTable.Add("Hello",1);
    MyHashTable.Add("Bye",2);

    this.Repeater1.DataSource=MyHashTable;
    this.Repeater1.DataBind();
    }

    private TextBox FindTextBox(Control container)
    {
    TextBox MyT = (TextBox) container.FindControl("TextBox1");

    //if (MyT == null)
    // return null;

    // Must anticipate the state restoration here. When this function executes we're
    // in the middle of the DataBind call on the given RepeaterItem object and
    // before the ItemDataBound event fires. At this time, the Checked property of the
    // CheckBox (as well as the Text property of a TextBox) has not been updated yet.
    //ctl.Checked = (Page.Request.Form[ctl.UniqueID]=="on");

    return MyT;
    }

    private void OnSubmit(object sender, EventArgs e)
    {
    TextBox MyT = (TextBox) Repeater1.Items[0].FindControl("TextBox1");
    Label1.Text=MyT.Text;
    }






    Customers

    Check and refresh to see more information




    <%# m_MyTextBox = (TextBox) FindTextBox(Container) %>







    Label


    [code]
Sign In or Register to comment.