动态表添加行ASP.NET? [英] Add row dynamically to table in ASP.NET?

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

问题描述

我想动态添加行到ASP.NET一个System.Web.UI.WebControls.Table控制。我发现了一些样品code ,做什么,我正在寻找,但只有它添加一行。

这似乎是表行数不进行从一个页面加载到下一个。用户应该可以添加任意多行根据需要,我需要从每一行获取的数据。

我在想什么?低于code。

 <%@页面语言=VB%GT;
<脚本=服务器>    子btnAddEmail_Click(BYVAL发件人为对象,BYVAL E上的EventArgs)        昏暗的TR作为新的TableRow
        昏暗tcLabel作为新的TableCell
        昏暗tcTextBox作为新的TableCell
        昏暗newLabel作为新标签
        昏暗newTextBox作为新文本框        昏暗我为整数= Table1.Rows.Count + 1        newLabel.ID =电子邮件+ Convert.ToString(I)
        newLabel.Text =电子邮件+ Convert.ToString(I)        newTextBox.ID =txtEmail+ Convert.ToString㈠        tcLabel.Controls.Add(newLabel)
        tcTextBox.Controls.Add(newTextBox)        tr.Cells.Add(tcLabel)
        tr.Cells.Add(tcTextBox)        Table1.Rows.Add(TR)    结束小组< / SCRIPT><表ID =form1的=服务器>
< D​​IV>
    < ASP:按钮=服务器文本=添加电子邮件ID =btnAddEmail
        的onclick =btnAddEmail_Click/>
    < ASP:表ID =表1=服务器>
        < ASP:&的TableRow GT;
            < ASP:TableCell的>
                < ASP:标签ID =EMAIL1=服务器文本=EMAIL1>< / ASP:标签>< / ASP:TableCell的>
            < ASP:TableCell的>
                < ASP:文本框ID =txtEmail1=服务器>< / ASP:文本框>< / ASP:TableCell的>
        < / ASP:&的TableRow GT;
    < / ASP:表>
< / DIV>
< /表及GT;


解决方案

ASP.NET是无状态的 - 你需要的页面负载之间你想要的任何数据持续到会话变量。

您需要将数据表/数据集存储在一个会话变量,并在你的pageLoad的事件指派表1到该数据集的DataSource属性。然后,在你的Click事件,该行添加到DataSet中的会话变量。

更新

您也许能够设置的ViewState =真正的你想拥有控制通过回传留存数据。

I'm trying to add rows dynamically to a System.Web.UI.WebControls.Table control in ASP.NET. I found some sample code that does what I'm seeking, but it only adds a single row.

It seems like the table row count does not carry from one page load to the next. The user should be able to add as many rows as desired, and I need to capture the data from each row.

What am I missing? Code below.

<%@ Page Language="VB" %>
<script runat="server">

    Sub btnAddEmail_Click(ByVal Sender As Object, ByVal e As EventArgs)

        Dim tr As New TableRow
        Dim tcLabel As New TableCell
        Dim tcTextBox As New TableCell
        Dim newLabel As New Label
        Dim newTextBox As New TextBox

        Dim i As Integer = Table1.Rows.Count + 1

        newLabel.ID = "Email" + Convert.ToString(i)
        newLabel.Text = "Email" + Convert.ToString(i)

        newTextBox.ID = "txtEmail" + Convert.ToString(i)

        tcLabel.Controls.Add(newLabel)
        tcTextBox.Controls.Add(newTextBox)

        tr.Cells.Add(tcLabel)
        tr.Cells.Add(tcTextBox)

        Table1.Rows.Add(tr)

    End Sub

</script>

<form id="form1" runat="server">
<div>
    <asp:Button runat="server" Text="Add Email" ID="btnAddEmail"
        onclick="btnAddEmail_Click" />
    <asp:Table ID="Table1" runat="server">
        <asp:TableRow>
            <asp:TableCell>
                <asp:Label ID="Email1" runat="server" Text="Email1"></asp:Label></asp:TableCell>
            <asp:TableCell>
                <asp:TextBox ID="txtEmail1" runat="server"></asp:TextBox></asp:TableCell>
        </asp:TableRow>
    </asp:Table>
</div>
</form>

解决方案

ASP.NET is stateless -- you need to persist any data you want between pages loads into a Session variable.

You'll need to store the DataTable/DataSet in a session variable, and assign the Datasource property of Table1 to that DataSet in your "PageLoad" event. Then, in your "Click" event, add the row to the DataSet in your Session variable.

update

You maybe able to set ViewState="true" on the control you wish to have persist data through postbacks.

这篇关于动态表添加行ASP.NET?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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