这是我的.aspx代码:
<asp:Panel ID="passengerBox" CssClass="transparentBox" runat="server">
<asp:Table ID="passengerDetailsTable" runat="server">
<asp:TableRow>
<asp:TableCell><asp:Label runat="server" Text="1." /></asp:TableCell>
<asp:TableCell><asp:TextBox ID="name1" CssClass="nameTextbox form-control" runat="server" /></asp:TableCell>
<asp:TableCell><asp:TextBox ID="age1" CssClass="ageTextbox form-control" runat="server" /></asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell><asp:Label runat="server" Text="2." /></asp:TableCell>
<asp:TableCell><asp:TextBox ID="name2" CssClass="nameTextbox form-control" runat="server" /></asp:TableCell>
<asp:TableCell><asp:TextBox ID="age2" CssClass="ageTextbox form-control" runat="server" /></asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell><asp:Label runat="server" Text="3." /></asp:TableCell>
<asp:TableCell><asp:TextBox ID="name3" CssClass="nameTextbox form-control" runat="server" /></asp:TableCell>
<asp:TableCell><asp:TextBox ID="age3" CssClass="ageTextbox form-control" runat="server" /></asp:TableCell>
</asp:TableRow>
</asp:Table>
<asp:Button ID="addPerson" runat="server" Text="Add Person" OnClick="addPerson_Click" />
</asp:Panel>
这是我的.aspx.cs页面:
public partial class Pages_Passengers : System.Web.UI.Page
{
int count = 3;
protected void addPerson_Click(object sender, EventArgs e)
{
TextBox nameTxtBox = new TextBox();
TextBox ageTxtBox = new TextBox();
TableRow tRow = new TableRow();
for (int i = 1; i <= 3; i++)
{
TableCell tCell = new TableCell();
if (i == 1)
tCell.Text = count.ToString() + ".";
if (i == 2)
tCell.Controls.Add(nameTxtBox);
if (i == 3)
tCell.Controls.Add(ageTxtBox);
tRow.Cells.Add(tCell);
}
passengerDetailsTable.Rows.Add(tRow);
}
}
每次点击按钮&#34; addPerson&#34;时,我想在表格中添加一个新行。但是这个按钮只能工作一次。我该怎么办?
答案 0 :(得分:0)
确定!我假设代码没有真正的用途,但它仅用于培训。
如果您没有使用数据库,那么获取数据持久性的简单方法就是使用会话变量(所以......它有效,但让我提醒您,这并不总是一个好主意)
网络表单:
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
</div>
</form>
您的网络表单现在更干净,因为该表将由后面的代码管理。
代码:
public partial class WebForm1 : System.Web.UI.Page
{
private Table _passengerDetailsTable;
private int _rowCounter = 0;
protected void Page_Load(object sender, EventArgs e)
{
// a recomandation ... make strong use of comments
this.InitTable(); // initializing the table from code behind
this._rowCounter = this._passengerDetailsTable.Rows.Count;
}
private void InitTable()
{
this._passengerDetailsTable = new Table();
if (Page.IsPostBack) // ok! if page is loaded for the first time we can create table, if we are back by a summit button code will be skipped
{
this._passengerDetailsTable = (Table)Session["tblPassengers"]; // get back the Table we stored in a session variable
}
else
{
TableRow dr = CreatePassengerDataRow(); // remember DRY Don't Repeat Yourself you are expected to create many rows. Bundle all the needed stuff inside a method
// perhaps you should create an header for the table to enhance readibility
this._passengerDetailsTable.Rows.Add(dr); // add the datarow
Session["tblPassengers"] = this._passengerDetailsTable; // store the table state in a session variable
}
this.form1.Controls.Add(_passengerDetailsTable); // add the control to the form
}
/// <summary>
/// create a new datarow
/// </summary>
/// <param name="counter"></param>
/// <returns></returns>
private TableRow CreatePassengerDataRow()
{
var dr = new TableRow(); // keeping things easy I'll create a single TableRow
// avoid the loop, it make code less readable and has no clear purpose
var tc0 = new TableCell(); // counter cell
tc0.Text = String.Format("#{0}", (this._rowCounter));
dr.Cells.Add(tc0);
var tc1 = new TableCell(); // name cell
tc1.Controls.Add(this.CreateInputTb("name", this._rowCounter)); // DRY again
dr.Cells.Add(tc1);
var tc2 = new TableCell(); // age cell
tc2.Controls.Add(this.CreateInputTb("age", this._rowCounter));
dr.Cells.Add(tc2);
return dr;
}
/// <summary>
///
/// </summary>
/// <param name="name"></param>
/// <param name="counter"></param>
/// <returns></returns>
private TextBox CreateInputTb(string name, int counter)
{
var textBox = new TextBox();
textBox.ID = String.Format("{0}_{1}", name, counter); // create a tb unique identifier (makes your life easier if you plan to use is data)
return textBox;
}
protected void Button1_Click(object sender, EventArgs e)
{
TableRow dataRow = this.CreatePassengerDataRow(); // create a brand new role
this._passengerDetailsTable.Rows.Add(dataRow); // append the role
}
}
享受。