codewarrior
codewarrior

Reputation: 193

add text box dynamically and capture data on button click

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);
}

Upvotes: 1

Views: 2469

Answers (2)

Win
Win

Reputation: 62301

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);
}

Upvotes: 1

gmail user
gmail user

Reputation: 2783

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)
{

}

Upvotes: 0

Related Questions