1

I am adding text boxes dynamically and trying to capture data entered in text box on button click. but what is happening is , though I entered the data in the text box, when I clicked the button, the page is getting loaded and the control is getting created again. As a result , I am loosing the data in the text box. Can you tell me how can I capture this data entered to the dynamically created text boxes. My sample code is as follows:

   protected void Page_Load(object sender, EventArgs e)
    {
        Table tblTextboxes = new Table();
        for(int i=0;i<10;i++)
        {
            TableRow tr=new TableRow();
            TableCell tc=new TableCell();
            TextBox tb=new TextBox();
            tb.ID=i.ToString();
            tc.Controls.Add(tb);
            tr.Cells.Add(tc);

            TableCell tc1=new TableCell();
            LinkButton lnk=new LinkButton();
            lnk.ID=i.ToString()+tb.Text+"lnk";
            lnk.Text = "Show";
            lnk.Click+=new EventHandler(lnk_Click);
            tc1.Controls.Add(lnk);

            tr.Cells.Add(tc1);

            tblTextboxes.Rows.Add(tr);
        }
        placeTest.Controls.Add(tblTextboxes);
    }



void  lnk_Click(object sender, EventArgs e)
{
LinkButton lnk=sender as LinkButton;
Label lbl=new Label();
lbl.Text="The text is"+lnk.ID;
placeTest.Controls.Add(lbl);
}

2 Answers 2

1

LinkButton ID is changed every time you enter text into TextBox and post back.

One thing you want to make sure when creating control dynamically is - you want to recreate them with same ID when post back.

Updated Solution (to retrieve text from TextBox)

protected void Page_Load(object sender, EventArgs e)
{
    var tblTextboxes = new Table();
    for (int i = 0; i < 10; i++)
    {
        var tr = new TableRow();
        var tc = new TableCell();
        var tb = new TextBox {ID = i.ToString()};
        tc.Controls.Add(tb);
        tr.Cells.Add(tc);

        var tc1 = new TableCell();
        // This is a fix for - lnk.ID=i.ToString()+tb.Text+"lnk";
        var lnk = new LinkButton {ID = i + "lnk", Text = "Show"};
        lnk.Click += lnk_Click;
        tc1.Controls.Add(lnk);

        tr.Cells.Add(tc1);

        tblTextboxes.Rows.Add(tr);
    }
    placeTest.Controls.Add(tblTextboxes);
}

void lnk_Click(object sender, EventArgs e)
{
    var lnk = sender as LinkButton;
    var lbl = new Label();
    lbl.Text = "LinkButton ID: " + lnk.ID;

    // Get number value from string
    string id = Regex.Replace(lnk.ID, @"[^\d]", "");

    // Retrieves a TextBox control by ID    
    var control = FindControlRecursive(Page, id);

    if (control != null)
    {
        var textbox = control as TextBox;
        lbl.Text += "; TextBox Text: " + textbox.Text;
    }

    placeTest.Controls.Add(lbl);
}

public Control FindControlRecursive(Control root, string id)
{
    if (root.ID == id)
        return root;

    return root.Controls.Cast<Control>()
        .Select(c => FindControlRecursive(c, id))
        .FirstOrDefault(c => c != null);
}
Sign up to request clarification or add additional context in comments.

2 Comments

how can I capture the text in the text box on button click.
I updated the answer. If this answer your question, please mark it as Answer so that others can use it too.
0

Based on the MSDN, I would recommend to use Page class's Init event. Look in to the title View State and Dynamically Added Controls for explanation. Also, I would recommend to add dynamic controls at the end of all existing controls. Create table first, and then add the text boxes.

I modified your code. It worked for me. I used VS 2012, .Net 4.5

protected void Page_Load(object sender, EventArgs e)
{
    Response.Write(DateTime.Now.ToString());

}
protected void Page_Init(object sender, EventArgs e)
{

    Table tblTextboxes = new Table();
    for (int i = 0; i < 10; i++)
    {
        TableRow tr = new TableRow();
        TableCell tc = new TableCell();
        TextBox tb = new TextBox();
        tb.ID = i.ToString();
        tc.Controls.Add(tb);
        tr.Cells.Add(tc);

        //TableCell tc1 = new TableCell();
        //LinkButton lnk = new LinkButton();
        //lnk.ID = i.ToString() + tb.Text + "lnk";
        //lnk.Text = "Show";
        //lnk.Click += new EventHandler(lnk_Click);
        //tc1.Controls.Add(lnk);

        //tr.Cells.Add(tc1);

        tblTextboxes.Rows.Add(tr);
    }
    placeTest.Controls.Add(tblTextboxes);

}

protected void Button1_Click(object sender, EventArgs e)
{

}

2 Comments

why did you comment out the addition of button. I want to play the value in the label on button click against each text box.
This is just a sample code, that'll try to address your problem. You can use it to build up your app. Just curious to know that, is this code working?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.