2012-12-02 91 views
5

我正在使用asp.net [c#] ..如何通过单击按钮添加新的ASP.NET表格行?

我的问题是关于添加新行;如果我点击该按钮(像我每次该按钮单击它会加入新行).. 我想它很容易做到这一点..但它不存在。有些东西缺失我不知道是什么。

我的代码为[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 ..我试过其他的方法,但没有工作..

请如果有人能帮助我..我觉得我失去了一些东西很重要,但我不知道它是什么..

回答

2

如在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); 
    } 
    

这将解决您的问题。

+0

thx很多我的朋友..这帮助我真正.. – NewStudent

+0

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

0

您只需通过添加一行:

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

但值得关注的是:

  1. 点击了按钮,行被添加到表中。
  2. 点击了同一个按钮再然后,你已经创建不再作为持续存在ASP.NET是无状态的第一行。

解决方案:确保每按一下按钮,你已经创建的行数据存在。

相关问题