您好,几天来我真的陷入了一项开发任务中,我无法弄清楚究竟发生了什么。请帮忙。
我正在尝试将行动态添加到网页,如下所示。
我想使用服务器控制。但我无法添加多于一行。
即使我使用了会话变量,也没有运气。请帮忙:)
------------------aspx文件---------------
<div>
<asp:Table ID="tbl" runat="server">
<asp:TableRow ID="rw0">
<asp:TableCell ID="c01" Width="100px">
<asp:CheckBox runat="server" ID="chk0" />
</asp:TableCell>
<asp:TableCell ID="c02" Width="100px">
<asp:TextBox runat="server" ID="txt0" />
</asp:TableCell></asp:TableRow>
<asp:TableRow ID="rw1">
<asp:TableCell ID="c11" Width="100px">
<asp:CheckBox ID="chk1" runat="server" />
</asp:TableCell><asp:TableCell ID="c12" Width="100px">
<asp:TextBox runat="server" ID="txt1" />
</asp:TableCell></asp:TableRow>
</asp:Table>
<asp:Button ID="btn1" runat="server" Text="Add Row" OnClick="addRow" />
</div>
----------------------C#背后的代码------------------------ ----
protected void addRow(object sender, EventArgs e)
{
int num_row = new int(); //checkpoint
num_row = (tbl.Rows).Count;
if (Session["tables"] != null)
{
tbl = (Table)Session["tables"];
}
TableRow row = new TableRow();
TableCell cell1 = new TableCell();
TableCell cell2 = new TableCell();
TextBox tb = new TextBox();
CheckBox cb = new CheckBox();
row.ID = "rw" + num_row;
cell1.ID = "c" + num_row + "1";
cell2.ID = "c" + num_row + "2";
tb.ID = "txt" + num_row;
tb.EnableViewState = true;
cb.ID = "chk" + num_row;
cell1.Controls.Add(tb);
cell2.Controls.Add(cb);
row.Cells.Add(cell1);
row.Cells.Add(cell2);
tbl.Rows.Add(row);
Session["tables"] = tbl;
}
您每次只能看到添加一行,因为,动态创建的控件将无法跨回发使用。
当您第一次单击添加行时,会发生回发,添加第三行。当您第二次单击“添加行”时,已添加的行将不可用,并且会再次添加新行。最后你每次都只能看到一行。
底线是每次回发时都必须重新创建动态添加的控件在 page_load 事件中,动态添加的服务器控制不会跨回发持续存在的原因
refer 为什么当控件未执行完整回发时,动态创建的用户控件会消失? https://stackoverflow.com/questions/10434350/why-dynamically-created-user-controls-disappear-when-controls-are-not-doing-full also
要维护动态添加的控件的视图状态,您必须为控件生成 id。当您在回发期间重新创建控件时,请使用相同的 id 重新创建它们,以便维护视图状态。
使用一些标准逻辑来生成控件的 id。希望这可以帮助。
Update:
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
int num_row = (int)Session["No_of_Rows"];
for (int i = 2; i < num_row; i++)
{
TableRow row = new TableRow();
TableCell cell1 = new TableCell();
TableCell cell2 = new TableCell();
TextBox tb = new TextBox();
CheckBox cb = new CheckBox();
row.ID = "rw" + i;
cell1.ID = "c" + i + "1";
cell2.ID = "c" + i + "2";
tb.ID = "txt" + i;
tb.EnableViewState = true;
cb.ID = "chk" + i;
cell1.Controls.Add(cb);
cell2.Controls.Add(tb);
row.Cells.Add(cell1);
row.Cells.Add(cell2);
tbl.Rows.Add(row);
}
}
else
{
Session["No_of_Rows"] = 2;
}
}
protected void addRow(object sender, EventArgs e)
{
int num_row = (int)Session["No_of_Rows"]+1;
TableRow row = new TableRow();
TableCell cell1 = new TableCell();
TableCell cell2 = new TableCell();
TextBox tb = new TextBox();
CheckBox cb = new CheckBox();
row.ID = "rw" + num_row;
cell1.ID = "c" + num_row + "1";
cell2.ID = "c" + num_row + "2";
tb.ID = "txt" + num_row;
tb.EnableViewState = true;
cb.ID = "chk" + num_row;
cell1.Controls.Add(cb);
cell2.Controls.Add(tb);
row.Cells.Add(cell1);
row.Cells.Add(cell2);
tbl.Rows.Add(row);
Session["No_of_Rows"] = tbl.Rows.Count;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)