下面是aspx中的控件。
<asp:UpdatePanel runat="server" ID="panel" ChildrenAsTriggers="false" UpdateMode="Conditional">
<ContentTemplate>
<asp:PlaceHolder runat="server" ID="holder"></asp:PlaceHolder>
<asp:Button runat="server" ID="bt" Text="Add" onclick="AddTxt" />
<asp:Button runat="server" ID="btn1" Text="Refresh" onclick="btn1_Click" />
</ContentTemplate>
当单击按钮添加时,我正在创建一个动态表。下面是 C# 代码
protected void AddTxt(object sender, EventArgs e)
{
int tblRows = 3;
int tblCols = 3;
Table tbl = new Table();
for (int i = 0; i < tblRows; i++)
{
TableRow tr = new TableRow();
for (int j = 0; j < tblCols; j++)
{
TableCell tc = new TableCell();
TextBox txtBox = new TextBox();
txtBox.ID = "txt" + i.ToString() + j.ToString();
//txtBox.TextChanged += new EventHandler(txt_TextChanged);
tc.Controls.Add(txtBox);
tr.Cells.Add(tc);
if (Session["ctls"] != null)
{
Session["ctls"] += ";t";
}
else
{
Session["ctls"] = "t";
}
}
tbl.Rows.Add(tr);
}
holder.Controls.Add(tbl);
panel.Update();
}
当单击刷新按钮时发生部分回发时,我无法获取用户在文本框中更新的值。控件将具有空文本。
protected void Page_Load(object sender, EventArgs e)
{
foreach (Control c in holder.Controls)
{
if (c is TextBox)
{
}
}
}
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
if (Session["ctls"] != null)
{
string[] ctls = Session["ctls"].ToString().Split(';');
foreach (string ctlType in ctls)
{
if (string.Compare(ctlType, "t") == 0)
{
holder.Controls.Add(new TextBox());
}
}
}
}
任何人都可以帮助/给我一个提示如何解决更新面板内的动态控件。