在LinkButton的点击文本框动态 [英] Dynamic TextBox on LinkButton click
问题描述
我通过点击一个LinkButton创建一个页面动态文本框。
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);
}
如果我把:
foreach (Control c in tagsPanel.Controls)
{
if (c is TextBox)
{
lblError.Text += c.ClientID + " , ";
}
}
在lbAddTag_Click方法,我可以看到的项目,他们的存在,但如果我提交页面,并尝试在数据库中没有插入值...
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...
任何暗示是非常AP preciated。
Any hint is much appreciated.
推荐答案
如果你动态地创建控件,他们没有坚持到控制层次/视图状态上提交。
If you create controls dynamically, they are not persisted into the controls hierarchy/view state on a submit.
检查这篇文章: http://www.4guysfromrolla.com/articles/092904- 1.aspx
我宁愿有一个Repeater控件,然后保持在会话一个ArrayList和使用,作为一个数据源到Repeater控件。当我想添加一行,我宁愿添加一行到会话中的ArrayList和重新绑定Repeater控件。
让我知道如果你需要一个例子。
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.
这篇关于在LinkButton的点击文本框动态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!