【问题标题】:How to add new ASP.NET table row by clicking button?如何通过单击按钮添加新的 ASP.NET 表行?
【发布时间】:2012-12-02 23:11:52
【问题描述】:

我正在使用 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..我尝试了其他方法但没有奏效..

请如果有人可以帮助我..我觉得错过了一些重要的东西,但我不知道它是什么..

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:

    Walid's answer 分享的问题中所述,请按照以下步骤操作:

    1. 创建表行的全局列表,例如:

      List<TableRow> TableRows
      
    2. 在按钮中单击将新创建的行添加到列表中:

      TableRow row1=new TableRow();
      TableRows.add(row1);
      
    3. OnInit 方法中,只需将所有行添加到表中:

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

    它会解决你的问题。

    【讨论】:

    • 出现错误-foreach 语句无法对 table 类型的变量进行操作,因为 table 不包含 getNumerator 的公共定义。
    【解决方案2】:

    您需要保持控件(表格)的状态。

    在这里ASP.NET dynamically created controls and Postback查看一个非常相似的问题的清晰解释

    【讨论】:

    • 你能帮我简单一下吗..我什么都不懂..[如果你不介意]让我的例子正常工作有什么不好的?
    【解决方案3】:

    您可以通过使用添加行:

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

    但问题是:

    1. 点击按钮,表格中增加一行。
    2. 再次单击同一个按钮,您已经创建的第一行不再存在,因为 ASP.NET 是无状态的。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-06-08
      • 1970-01-01
      • 2013-12-24
      • 2020-12-16
      • 2019-07-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多