如何通过点击按钮添加新的ASP.NET表行? [英] How to add new ASP.NET table row by clicking button?

查看:158
本文介绍了如何通过点击按钮添加新的ASP.NET表行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用asp.net [C#] ..
恩,如果我那个按钮单击我的Q值约增加新行[像我每次点击该按钮,将添加新行] ..
我想它很容易做到这一点..但它不存在缺了点什么IDK的是什么..; /

am using asp.net [ c# ] .. well my Q 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 not there something missing idk what .. ;/

我的code是[Default3.aspx]:

my code is [ 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]:

and for my 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);


}

IDK的是什么问题..我甚至给每一个控制一个的ID ..我试过其他的方法,但没有奏效。

idk what the problem is .. i even give each controls an IDs .. and i tried other ways but didn't work ..

PLZ如果有人能帮助我..我觉得像我失去了一些东西很重要,但IDK的是什么。

plz if anyone can help me out .. i feel like am missing something important but idk what it is ..

推荐答案

由于在瓦利德的回答 ,请按照下列步骤操作:

As given in the Question shared in Walid's answer, follow these steps:


  1. 创建表行的全局列表,是这样的:

  1. Create a global list of table rows, something like:

List<TableRow> TableRows


  • 在按钮,单击添加新创建的行列出:

  • In button click Add the newly created row to list:

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


  • 的OnInit 方法只是所有的行添加到表:

  • In the OnInit method simply add all the rows to the table:

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


  • 这将解决您的问题。

    这篇关于如何通过点击按钮添加新的ASP.NET表行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆