嗨,自从几天以来,我真的陷入了我的一项开发任务中,我无法找出到底发生了什么。请帮忙。
我正在尝试将行动态添加到网页中,如下所示。我想使用服务器控制。但我无法添加多于一行。
即使我使用了会话变量,也没有运气。请帮助:)
-----------------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;
}