C# 添加重复行&;单击c上的列#
我有一个ASP:表,其中有一行包含单元格,我需要能够通过单击末尾的“添加”按钮向其中添加一行,该按钮将复制现有行 我的表格代码C# 添加重复行&;单击c上的列#,c#,asp.net,C#,Asp.net,我有一个ASP:表,其中有一行包含单元格,我需要能够通过单击末尾的“添加”按钮向其中添加一行,该按钮将复制现有行 我的表格代码 <asp:Table ID="Table1" runat="server" Height="50%" Width="100%"> <asp:TableHeaderRow CssClass="lblrow2" HorizontalAlign="Left" BackColor="AliceBlue">
<asp:Table ID="Table1" runat="server" Height="50%" Width="100%">
<asp:TableHeaderRow CssClass="lblrow2" HorizontalAlign="Left" BackColor="AliceBlue">
<asp:TableHeaderCell ID="haccountref">Account Ref:</asp:TableHeaderCell>
<asp:TableHeaderCell ID="hproduct">Product</asp:TableHeaderCell>
<asp:TableHeaderCell ID="hqty">Qty:</asp:TableHeaderCell>
<asp:TableHeaderCell ID="hunitprice">Unit Price:</asp:TableHeaderCell>
<asp:TableHeaderCell ID="hdiscount">Discount:</asp:TableHeaderCell>
<asp:TableHeaderCell ID="htotal">Total Line Amount:</asp:TableHeaderCell>
</asp:TableHeaderRow>
<asp:TableRow CssClass="r1">
<asp:TableCell><asp:TextBox ID="vaccountref" ReadOnly="True" runat="server"></asp:TextBox></asp:TableCell>
<asp:TableCell><asp:DropDownList runat="server" ID="vproduct"></asp:DropDownList></asp:TableCell>
<asp:TableCell><input id="Qty" type="text" /></asp:TableCell>
<asp:TableCell><input id="Unit Price" type="text" /></asp:TableCell>
<asp:TableCell><input id="Discount" type="text" /></asp:TableCell>
<asp:TableCell><input id="Total" type="text" /></asp:TableCell>
<asp:TableCell><input id="Button1" type="image" src="plusButton.png" value="button" /></asp:TableCell>
</asp:TableRow>
</asp:Table>
账户编号:
产品
数量:
单价:
折扣:
总行数:
我的c#代码落后-我没有收到任何错误,但没有重复的行
protected void Button1_Click(object sender, System.EventArgs e)
{
// Total number of rows.
int rowCnt;
// Current row count.
int rowCtr;
// Total number of cells per row (columns).
int cellCtr;
// Current cell counter
int cellCnt;
rowCnt = int.Parse(TextBox1.Text);
cellCnt = int.Parse(TextBox2.Text);
for (rowCtr = 1; rowCtr <= rowCnt; rowCtr++)
{
// Create new row and add it to the table.
TableRow tRow = new TableRow();
Table1.Rows.Add(tRow);
for (cellCtr = 1; cellCtr <= cellCnt; cellCtr++)
{
// Create a new cell and add it to the row.
TableCell tCell = new TableCell();
tCell.Text = "Row " + rowCtr + ", Cell " + cellCtr;
tRow.Cells.Add(tCell);
}
}
}
受保护的无效按钮1\u单击(对象发送者,System.EventArgs e)
{
//总行数。
int rowCnt;
//当前行计数。
int rowCtr;
//每行(列)的单元格总数。
int cellCtr;
//电流单元计数器
int-cellCnt;
rowCnt=int.Parse(TextBox1.Text);
cellCnt=int.Parse(TextBox2.Text);
对于(rowCtr=1;rowCtr请确保您实际使用的是ASP.NET服务器控件按钮。您当前使用的是HTML输入图像/按钮。服务器不会发回以捕获该事件
使用
服务器控件尝试将此列作为表中的最后一列
<asp:TableCell>
<asp:ImageButton id="Button1" runat="server"
ImageUrl="plusButton.png" OnClick="Button1_Click" />
</asp:TableCell>
这应该会取代:
谢谢,但是我意识到这段代码并没有达到我想要的效果,我需要在行中重复这些字段,因为这一页是为了让用户输入多个值以写入数据库table@Justin:您正在使用MSDN中的示例代码。问题中的代码出现的原因是s不复制行是因为您没有使用服务器控件。我建议您创建一个新问题,以询问有关复制数据的下一个问题。您是否尝试将这一行-Table1.Rows.Add(tRow)放在下面;将单元格添加到行中后,即将其作为for中的最后一行look@Subhash-Dike这是可行的,但是我想复制我已经设置好的整行,这样用户可以在行添加时输入另一行文本,但只使用文本。这里的想法是,用户可以填写一个订单列表,然后进行排序提交给sql表