点击按钮如何添加新的ASP.NET表格行?

5

我是一名IT相关人员,需要为您翻译一段与ASP.NET [ C# ] 相关的内容。

我的问题是如何添加新行。每次点击该按钮时(每次都会添加新行)…我认为这很容易做到...但事实并非如此。我不知道缺少了什么东西。

我的代码是[ Default3.aspx ]:

<%@ Page Language="C#" AutoEventWireup="true"
    CodeFile="Default3.aspx.cs" Inherits="Default3" %>

<!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 align="center">     

<asp:Table ID="Table1" runat="server">
    <asp:TableRow>
        <asp:TableCell style="border-style:solid" >
           <asp:Label ID="Label1" runat="server" Text="LABEL = 1 ">
           </asp:Label>
        </asp:TableCell>
        <asp:TableCell style="border-style:solid" >
           <asp:Label ID="Label2" runat="server" Text="LABEL = 2 ">
           </asp:Label>
        </asp:TableCell>
    </asp:TableRow>
    <asp:TableRow>
        <asp:TableCell style="border-style:solid" >
           <asp:Label ID="Label3" runat="server" Text="LABEL = 3 ">
           </asp:Label>
        </asp:TableCell>
        <asp:TableCell style="border-style:solid" >
           <asp:Label ID="Label4" runat="server" Text="LABEL = 4 ">
           </asp:Label>
        </asp:TableCell>
    </asp:TableRow>
</asp:Table>

<asp:Button ID="Button1" runat="server" Text="Add More" 
        onclick="Button1_Click" />
</div>
</form>

</body>
</html>

对于我的 C# [ Default3.aspx.cs]:

protected void Button1_Click(object sender, EventArgs e)
{

    TableRow NewRow1 = new TableRow();

    //1st cell
    TableCell NewCell1 = new TableCell();
    NewCell1.Style.Add("border-style","solid");

    // new lebel
    Label newLable1 = new Label();
    count = count + 1; // just for change number in label text 
    newLable1.Text = "NewLabel = "+ count;

    // adding lebel into cell
    NewCell1.Controls.Add(newLable1);

    // adding cells to row
    NewRow1.Cells.Add(NewCell1);

    //2ed cell
    TableCell NewCell2 = new TableCell();
    NewCell2.Style.Add("border-style", "solid");

    Label newLable2 = new Label();
    count = count + 1;
    newLable2.Text = "NewLabel = " + count;
    NewCell2.Controls.Add(newLable2);
    NewRow1.Cells.Add(NewCell2);

    //adding row into table
    Table1.Rows.Add(NewRow1);


}

我不知道问题出在哪里..我甚至为每个控件都分配了一个ID..我尝试了其他方法,但都没有起作用..

如果有人能帮助我解决问题,我会非常感激..我感觉自己缺少了一些重要的东西,但又不知道是什么..

3个回答

2

根据Walid的回答中提供的问题,按照以下步骤进行:

  1. Create a global list of table rows, something like:

    List<TableRow> TableRows
    
  2. In button click Add the newly created row to list:

    TableRow row1=new TableRow();
    TableRows.add(row1);
    
  3. In the OnInit method simply add all the rows to the table:

    foreach ( TableRow row in TableRows )
    {
        Table1.Rows.Add(row);
    }
    

它将解决您的问题。


获取错误-foreach语句无法操作类型为table的变量,因为table不包含getNumerator的公共定义。 - Azhar Shahid

2

你能简单地解释一下吗?我一个也没懂。如果您不介意的话,我该怎么做才能让我的示例正常工作呢? - NewStudent

0

您可以通过以下方式添加行:

TableRow row1=new TableRow();
TableRows.add(row1);

但问题是:

  1. 点击按钮后,会向表中添加一行。
  2. 再次点击相同的按钮,之前创建的第一行将不再存在,因为ASP.NET是无状态的。

解决方案:确保在每次按钮点击时,您已经创建的行数据仍然存在。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接