添加重复行&列OnClick c#

时间:2011-03-28 22:28:08

标签: c# asp.net

我有一个ASP:表格中有一行包含单元格我需要能够通过单击末尾的“添加”按钮向其添加一行,这会复制现有行。

我的表格代码

<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 个答案:

答案 0 :(得分:0)

确保您实际使用的是ASP.NET服务器控件按钮。您当前正在使用HTML输入图像/按钮。它不会回发给服务器捕获该事件。

尝试使用<asp:ImageButton>服务器控件作为表格中的最后一列。

<asp:TableCell>
    <asp:ImageButton id="Button1"  runat="server" 
         ImageUrl="plusButton.png"   OnClick="Button1_Click" />
</asp:TableCell>

应该替换:<input id="Button1" />