如何检索动态表中的控件内容?

发布于 2024-09-16 04:10:05 字数 121 浏览 10 评论 0原文

我有一个页面,我想收集有关 x 个用户的信息。我有一个控件,您可以在其中输入用户数,并根据该数字创建一个动态表,其中每个用户都有一行。每个表行都有文本框控件,我想在回发时从中检索值。如何才能做到这一点?

I have a page where I would like to collect information about x number of users. I have a control where you enter in the number of users and based off of that number, I create a dynamic table with a row for each user. Each table row has textbox controls that I would like to retrieve the value from on postback. How can this be accomplished?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

街角卖回忆 2024-09-23 04:10:05

您的目标是哪种特定风格的 ASP.Net? ASP.NET MVC?网络表格?

在 Web 表单(我更熟悉)中执行此类操作的最快、最简单的方法是在页面上放置一个 GridView 控件,并将一个通用集合绑定到它,您可以根据输入的数字设置其大小控制。

这是一段 10m 的快速代码。在 Visual Studio 2010 中创建了一个默认的 WebForms Web 项目。

网页来源:

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">

    <p>
        <table>
            <tr>
                <td>Rows:</td>
                <td><asp:TextBox ID="TextBox1" runat="server" />
                    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
                </td>
            </tr>
            <tr>
                <td colspan=2>
                <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
                    <Columns>
                        <asp:TemplateField>
                            <ItemTemplate>
                                <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>
                    <asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="Button" />
                </td>
            </tr>
        </table>


    </p>

</asp:Content>

隐藏代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Init(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            List<string> users = new List<string>(Enumerable.Repeat(string.Empty, Int32.Parse(TextBox1.Text)));
            GridView1.DataSource = users;
            GridView1.DataBind();
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            var list = from GridViewRow row in GridView1.Rows
                       where row.RowType == DataControlRowType.DataRow
                       select (row.FindControl("TextBox2") as TextBox).Text;
            // now do something with this list of strings
        }
    }
}

What particular style of ASP.Net are you targeting? ASP.Net MVC? Webforms?

The quickest and easiest way to do something like this in webforms (which Im way more familiar with) would be to drop a GridView control on the page, and bind a generic collection to it that you set the size of based on the number entered in the control.

Here's a quick 10m piece of code. Created a default WebForms Web project in Visual Studio 2010.

Web Page Source:

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">

    <p>
        <table>
            <tr>
                <td>Rows:</td>
                <td><asp:TextBox ID="TextBox1" runat="server" />
                    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
                </td>
            </tr>
            <tr>
                <td colspan=2>
                <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
                    <Columns>
                        <asp:TemplateField>
                            <ItemTemplate>
                                <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>
                    <asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="Button" />
                </td>
            </tr>
        </table>


    </p>

</asp:Content>

Code behind:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Init(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            List<string> users = new List<string>(Enumerable.Repeat(string.Empty, Int32.Parse(TextBox1.Text)));
            GridView1.DataSource = users;
            GridView1.DataBind();
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            var list = from GridViewRow row in GridView1.Rows
                       where row.RowType == DataControlRowType.DataRow
                       select (row.FindControl("TextBox2") as TextBox).Text;
            // now do something with this list of strings
        }
    }
}
不知在何时 2024-09-23 04:10:05

您可能会发现创建 asp:GridView 更容易。然后,您可以迭代回发时的行并检查控件。那里有很多示例代码。

You may find it much easier to create an asp:GridView instead. You can then iterate through the rows on postback and inspect the controls. Lots of example code out there.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文