简体   繁体   English

如何通过单击按钮添加新的ASP.NET表行?

[英]How to add new ASP.NET table row by clicking button?

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 .. 如果有人可以帮助我...我觉得我错过了一些重要但我不知道它是什么..

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动态创建控件和回发

As given in the Question shared in Walid's answer , follow these steps: 正如在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: OnInit方法中,只需将所有行添加到表中:

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

It will solve your problem. 它会解决你的问题。

You can add row just by using: 您可以使用以下方法添加行:

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

But the concern is: 但关注的是:

  1. Clicked on Button, a row is added to table. 单击Button,将一行添加到表中。
  2. 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. 解决方案:确保在每次单击按钮时,您已创建的行数据都存在。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在不使用asp.net刷新页面的情况下使用添加按钮向现有表中动态添加行? - How to dynamically add row to existing table with add button and without page being refreshed in asp.net? 如何在ASP.Net网页上使用OleDb向数据库添加新行 - How to add new row to database with OleDb on a ASP.Net webpage 如何通过单击 ASP.NET gridview 中一行中的按钮获取行数据 - How to get row data by clicking a button in a row in an ASP.NET gridview 如何通过单击ASP.NET gridview中一行中的图像按钮来获取行数据 - How to get row data by clicking a image button in a row in an ASP.NET gridview 如何在ASP.NET中使用HTML表格动态添加按钮 - How to dynamically add a button using html table in asp.net 通过单击 asp.net 中的按钮在网格中添加多行 - add Multiple rows in grid by clicking on button in asp.net 如何通过单击asp.net c#中的链接按钮来打开带有会话的新标签? - how to open a new tab with session by clicking link button in asp.net c#? 单击 asp.net mvc 或 jquery 中的按钮时如何创建新表单? - How to create new form when clicking button in asp.net mvc or jquery? ASP.NET MVC 5-通过单击“添加行”按钮可以处理IEnumerable的任意数量的元素的“创建”视图? - ASP.NET MVC 5 - “Create” view that can handle any number of elements for an IEnumerable based on clicking an “Add row” button? asp.net的核心是在视图中显示表格,每行旁边都有按钮。 单击按钮后,对数据库进行更改 - asp.net the core displays the table in a view with buttons next to each row. after clicking the button makes changes to the database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM