问题描述
实际上,我在 Pageload
上创建 1 个 TextBox
并将该 TextBox
添加到 Panel
.现在,我有一个 LinkButton
,例如 Add Another
.
Actually, I am Creating 1 TextBox
on Pageload
and adding that TextBox
to Panel
.
Now, I have a LinkButton
like Add Another
.
我正在该 TextBox
中输入文本,如果需要,我需要通过单击 Add Another LinkButton
来创建新的 TextBox
.
I am entering Text in that TextBox
and if needed I need to Create New TextBox
,by clicking Add Another LinkButton
.
实际上,我可以获取计数并重新创建 TextBoxes
.但是,问题是,我在以前生成的 Textboxes
中输入的文本丢失了.
Actually, I am able to get the count and recreate the TextBoxes
.
But,the Problem is that, My Entered text in the Previously Generated Textboxes
is Missing.
任何人都可以给我一个解决方案吗?
Can Anyone,Suggest me a solution for this?
protected void Page_Load(object sender, EventArgs e)
{
try
{
if (!IsPostBack)
{
for (int i = 0; i < 5; i++)
{
TableRow row = new TableRow();
for (int j = 0; j < 5; j++)
{
TableCell cell = new TableCell();
TextBox tb = new TextBox();
tb.ID = "TextBoxRow_" + i + "Col_" + j;
cell.Controls.Add(tb);
row.Cells.Add(cell);
}
Table1.Rows.Add(row);
}
}
}
catch (Exception ex)
{
throw;
}
}
这是一个示例代码,同样的代码写在 Button_Click
也
This is a Sample Code, the same code is written in Button_Click
Also
protected void ASPxButton1_Click(object sender, EventArgs e)
{
int k = Table1.Controls.Count;
}
我在 Button_Click
上得到一个 Count=0
.
I am getting a Count=0
on Button_Click
.
推荐答案
您只需在每次回发期间在页面加载事件之前或之内重新实例化/重新初始化动态控件并将此控件添加到页面/表单/占位符.然后,通过父控件调用LoadPostData方法,将发布的数据自动分配给控件.
All you need to do is to re-instantiate / reinitialize dynamic controls before or within page load event each and every time during postback and add this control to page / forms / placeholders. Then, the posted data will automatically be assigned to the control by calling the LoadPostData method by the parent control.
查看文章以及如何编写动态控制代码 -如何维护动态控制事件,asp.net中回发期间的数据
check the article and how to write code for dynamic control - How to maintain dynamic control events, data during postback in asp.net
这篇关于回发后动态创建的控件丢失数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!