1
我試圖在點擊按鈕添加行時向HtmlTable添加一個新行。 它增加了一行。但它不會增加更多行。請指教。從asp.net代碼添加行到htmltable
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Test.aspx.cs" Inherits="eLaundrySearchWeb.Test" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table id="myTable" runat="server">
<tr>
<td>
<asp:DropDownList ID="DropDownList1" runat="server">
</asp:DropDownList>
</td>
<td>
<asp:TextBox ID="txtQty" runat="server">
</asp:TextBox>
</td>
<td>
<asp:Button ID="Button1" runat="server" Text="Add Row" OnClick="AddRow_Click"
/>
</td>
</tr>
</table>
</div>
</form>
protected void AddRow_Click(object sender, EventArgs e)
{
//Random r=new Random();
//int rv=r.Next(0,100);
int numOfRows = myTable.Rows.Count;
HtmlTableRow row = new HtmlTableRow();
row.ID = "tbl_row"+(numOfRows+1);
HtmlTableCell cell1 = new HtmlTableCell();
cell1.ID = "tbl_cell1"+ (numOfRows+1);
TableCell cell2 = new TableCell();
cell2.ID = "tbl_cell2" + (numOfRows + 1);
TextBox tb = new TextBox();
tb.ID = "tbQty" + (numOfRows + 1);
cell1.Controls.Add(tb);
row.Cells.Add(cell1);
myTable.Rows.Add(row);
myTable.DataBind();
}
看起來像我需要重新處理的所有行。第一次點擊=>添加1行。第二次點擊=>在 – crazyTechie
@crazyTechie Ya上添加2行......所以你需要使用Viewstate來跟蹤計數,你可以看看我提到的問題,並且可能會這樣做。 – freebird