C# 如何通过单击按钮添加新的 ASP.NET 表行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13675015/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add new ASP.NET table row by clicking button?
提问by NewStudent
I am using asp.net [ c# ] ..
我正在使用 asp.net [ c# ] ..
My question is about adding new row; if I click on that button (like every time I click on that button it will add new row) .. I thought its easy to do it .. but it is not there. Something is missing I don't know what.
我的问题是关于添加新行;如果我点击那个按钮(就像每次我点击那个按钮时它都会添加新行)..我认为它很容易做到..但它不存在。东西少了我不知道是什么。
My code is [ Default3.aspx ] :
我的代码是 [ 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>
and for my C# [ Default3.aspx.cs ] :
和我的 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);
}
I don't know what the problem is .. I even give each controls an IDs .. and I tried other ways but didn't work ..
我不知道问题是什么.. 我什至给每个控件一个 ID.. 我尝试了其他方法但没有奏效..
Please if anyone can help me out .. I feel like am missing something important but I don't know what it is ..
请如果有人可以帮助我.. 我觉得错过了一些重要的东西,但我不知道它是什么..
采纳答案by Mohit
As given in the Question shared in Walid's answer, follow these steps:
正如Walid's answer 中共享的问题中所述,请按照以下步骤操作:
Create a global list of table rows, something like:
List<TableRow> TableRowsIn button click Add the newly created row to list:
TableRow row1=new TableRow(); TableRows.add(row1);In the
OnInitmethod simply add all the rows to the table:foreach ( TableRow row in TableRows ) { Table1.Rows.Add(row); }
创建表行的全局列表,例如:
List<TableRow> TableRows在按钮中单击将新创建的行添加到列表中:
TableRow row1=new TableRow(); TableRows.add(row1);在该
OnInit方法中,只需将所有行添加到表中:foreach ( TableRow row in TableRows ) { Table1.Rows.Add(row); }
It will solve your problem.
它会解决你的问题。
回答by Walid
You will need to persist the state of your control (table).
您需要保持控件(表)的状态。
See a clear explanation to a very similar problem here ASP.NET dynamically created controls and Postback
在这里看到对一个非常相似的问题的清晰解释 ASP.NET dynamic created controls and Postback
回答by Pavan Kumar
You can add row just by using:
您可以使用以下方法添加行:
TableRow row1=new TableRow();
TableRows.add(row1);
But the concern is:
但令人担忧的是:
- Clicked on Button, a row is added to table.
- Clicked on the same button again then first row which you have already created no longer persists as ASP.NET is Stateless.
- 单击按钮,将向表中添加一行。
- 再次单击同一个按钮,您已经创建的第一行不再存在,因为 ASP.NET 是无状态的。
Solution: Ensure that on every button click, your already created rows data exists.
解决方案:确保在每次单击按钮时,您已经创建的行数据都存在。

