Dynamic TextBox on LinkButton click

2019-07-21 22:10发布

I am creating dynamic TextBoxes in a page by clicking a LinkButton.

However, after that, if the page is submitted, I can't find the items created dynamically, thus, can't send the information to the database.

protected void lbAddTag_Click(object sender, EventArgs e)
{
   for (int i = 0; i < 3;i++ )
    {
        CreateTextBox("txtTag-" + i.ToString());
    }

}

private void CreateTextBox(string ID)
{
    TextBox txt = new TextBox();
    txt.ID = ID;
    txt.Width = Unit.Pixel(300);
    //txt.TextChanged += new EventHandler(OnTextChanged);
    txt.AutoPostBack = false;
    tagsPanel.Controls.Add(txt);

    Literal lt = new Literal();
    lt.Text = "<br /><br />";
    tagsPanel.Controls.Add(lt);
}

If I put:

foreach (Control c in tagsPanel.Controls)
{
    if (c is TextBox)
    {
        lblError.Text += c.ClientID + "  ,  ";
    }
}

in the lbAddTag_Click method I can see the items, and they exist, but if I submit the page and try to insert the values in the database nothing...

Any hint is much appreciated.

4条回答
别忘想泡老子
2楼-- · 2019-07-21 22:46

If you create controls dynamically, they are not persisted into the controls hierarchy/view state on a submit.

Check this article: http://www.4guysfromrolla.com/articles/092904-1.aspx

I would rather have a Repeater Control and then maintain an ArrayList in the session and use that as a DataSource to repeater Control. When I want to add a row I would rather add a row to the ArrayList in session and rebind the repeater control. Let me know if you need an example.

查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-07-21 22:50

Page life cycle stuff is really tricky in ASP.NET. You need to make sure the LinkButtons get created the same way before the life cycle tries to load in their view states. The best way to do this is probably to set up your control/page so that when it goes to save the ViewState, it first adds information about the textboxes that you've created to the ViewState. Then, immediately after loading the ViewState, create the textboxes based on that information. You can do this by overriding the SaveViewState and LoadViewState methods.

查看更多
4楼-- · 2019-07-21 22:55

The Controls Collection is a tree structure. Controls contain other controls, so it may have 1 Panel control, which contains several other Panels and those contain the TextBoxes you are looking for. You have to recursively go through the controls.

Look at this http://msdn.microsoft.com/en-us/library/486wc64h.aspx

Or this:

public delegate void control_visitor(Control control);

public void iterate_controls(ControlCollection controls, control_visitor visitor)
{
    foreach (Control control in controls)
    {
        visitor(control);
        iterate_controls(control.Controls, visitor);
    }
}
查看更多
时光不老,我们不散
5楼-- · 2019-07-21 23:01

You have to recreate every dynamic control on Postback, otherwise their values cannot be rebuilt from ViewState. So you need to save the count of your Textboxes (f.e. as ViewState variable). They need to get the same id so you should use the counter as suffix from the ID(f.e. TextBox_4).

查看更多
登录 后发表回答