只需单击按钮即可添加文本框行

时间:2011-12-05 06:59:04

标签: c# asp.net dynamic row

我很难坚持这个问题,并会感激任何帮助!我必须创建一个页面,用户可以通过单击按钮添加更多行。例如,我的第一行有2个文本框(名称和出生日期),第二行有“添加行”按钮。当用户单击“添加行”按钮时,应克隆并重复第一行...但用户不能添加超过5行。之后,所有信息都需要保存在SQL表中。如何在C#中实现这一目标?

我正在附加我的示例ASP.NET。谁能帮帮我吗?

PS:我已经阅读并试过“如何:动态地将行和单元格添加到表Web服务器控件”....但这对我不起作用。

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Testing Adding Rows</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>    
        <asp:Table ID="Table1" runat="server" width="400" style="border:[none][0]; border-color:White; border-style:hidden">                
            <asp:TableRow ID="TableRow1" runat="server">
                <asp:TableCell runat="server" nowrap="nowrap" Width= "70">
                    <asp:Label ID="nameLabel" runat="server" Text="Your Name" Font-Size="X-Small"></asp:Label>
                </asp:TableCell>
                <asp:TableCell runat="server" nowrap="nowrap" Width= "100">
                    <asp:TextBox ID="tb_name" runat="server" Font-Size="Smaller"></asp:TextBox> 
                    <asp:RequiredFieldValidator ID="nameValidator" runat="server" ControlToValidate="tb_name" Font-Size="Smaller">*</asp:RequiredFieldValidator>
                </asp:TableCell> 

                <asp:TableCell runat="server" nowrap="nowrap" Width= "70">
                    <asp:Label ID="dateLabel" runat="server" Text="Birthdate" Font-Size="Smaller" ></asp:Label>
                </asp:TableCell>                        
                <asp:TableCell runat="server" Width= "100">
                    <asp:TextBox ID="tb_date" runat="server" Font-Size="Smaller"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="dateValidator" runat="server" ControlToValidate="tb_date" Font-Size="Smaller">*</asp:RequiredFieldValidator>
                </asp:TableCell>

            </asp:TableRow>


            <asp:TableRow ID="TableRow2" runat="server"> 
                <asp:TableCell runat="server" align="left" Width= "100">
                    <asp:Button ID="addRow" runat="server" Height="22px" Text="Add Row" 
                                 ToolTip="Click to add another row" onclick="ButtonAddRow_Click" />
                </asp:TableCell> 
            </asp:TableRow> 


            <asp:TableRow ID="TableRow3" runat="server">  
                <asp:TableCell runat="server" bordercolor="#FFFFFF">    </asp:TableCell>

                <asp:TableCell runat="server"  align="left" nowrap="nowrap" bordercolor="#FFFFFF">
                    <asp:Label ID="msg" runat="server" ForeColor="Red" Font-Size="Smaller"></asp:Label>
                    <asp:ValidationSummary ID="LogonValidationSummary" HeaderText="All the fields (*) are required." DisplayMode="SingleParagraph"
                                           Font-Italic="true" ShowSummary="True" EnableClientScript="true" runat="server" Font-Size="Smaller"/> 
                </asp:TableCell>
            </asp:TableRow>

             <asp:TableRow ID="TableRow4" runat="server"> 
                <asp:TableCell ID="TableCell10" runat="server" bordercolor="#FFFFFF">       </asp:TableCell>

                <asp:TableCell ID="TableCell11" runat="server" align="left" bordercolor="#FFFFFF">
                    <asp:Button ID="ButtonSubmit" runat="server" Height="22px" Text="Submit" Width="79px" onclick="ButtonSubmit_Click" />
                </asp:TableCell>
            </asp:TableRow>

        </asp:Table>    
    </div>
    </form>
</body>
</html>

2 个答案:

答案 0 :(得分:1)

此代码段只是提示您可以这样做..

    TableRow row = new TableRow();
     for (int j = 0; j < colsCount; j++)
     {
        TableCell cell = new TableCell();
        TextBox tb = new TextBox();
           tb.ID = "TextBoxRow_" + i + "Col_" + j;
           cell.Controls.Add(tb);
           row.Cells.Add(cell);
      }

     Table1.Rows.Add(row);

答案 1 :(得分:0)

我想如果你使用GridView控件,而不是将行运行时添加到gridview而不是使用表。这样可以最大限度地减少你的努力。

这里有代码:Adding Dynamic Rows in GridView with TextBoxes enter image description here