![](/img/trans.png)
[英]How to dynamically add row to existing table with add button and without page being refreshed in asp.net?
[英]How to add new ASP.NET table row by clicking button?
我正在使用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 ..我嘗試了其他方法,但沒有工作..
如果有人可以幫助我...我覺得我錯過了一些重要但我不知道它是什么..
您需要保持控件的狀態(表)。
在這里看到一個非常類似的問題的清晰解釋ASP.NET動態創建控件和回發
您可以使用以下方法添加行:
TableRow row1=new TableRow();
TableRows.add(row1);
但關注的是:
解決方案:確保在每次單擊按鈕時,您已創建的行數據都存在。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.