views:

44

answers:

2

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?

A: 

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.

Bill
@Bill That was my next plan of attack. With a strong ASP classic background, I built the page with the assumption that it wouldn't be so difficult to reference dynamically created controls.
zeroef
A: 

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
        }
    }
}
Kilanash
@Kilanash - I'm using Webforms. Your example looks close to what I'm writing up now based off of Bill's suggestion to use a gridview.
zeroef