怎么才能在一个页面中动态生成几个TextBox,然后把值取回来1.TextBox的个数不是固定的.
2.能区别取回的值是哪一个TextBox的.有高手的请指教,如果有代码参考的,本人将会感激不尽

解决方案 »

  1.   

    代码如下,我那边测试通过.你COPY过去可要作小小修改:WebForm1.aspx
    -------------------------------------------------------------------------------
    <%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="WebApplication1.WebForm1" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    <HTML>
    <HEAD>
    <title>WebForm1</title>
    <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
    <meta name="CODE_LANGUAGE" Content="C#">
    <meta name="vs_defaultClientScript" content="JavaScript">
    <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
    </HEAD>
    <body MS_POSITIONING="GridLayout">
    <form id="Form1" method="post" runat="server">
    <asp:Button ID="btn_test" Runat="server" Text="显示"></asp:Button>
    </form>
    </body>
    </HTML>
    -------------------------------------------------------------------------------
    WebForm1.aspx.cs
    -------------------------------------------------------------------------------
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;namespace WebApplication1
    {
    /// <summary>
    /// WebForm1 的摘要说明。
    /// </summary>
    public class WebForm1 : System.Web.UI.Page
    {
    protected System.Web.UI.WebControls.Button btn_test;

    private void Page_Load(object sender, System.EventArgs e)
    {
    TextBox oTxb;
    for (int i=0;i<5;i++)
    {
    oTxb = new TextBox();
    oTxb.ID="txb_" + i.ToString();
    Page.Controls[1].Controls.Add(oTxb);
    }
    } #region Web 窗体设计器生成的代码
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
    //
    InitializeComponent();
    base.OnInit(e);
    }

    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {    
    this.btn_test.Click += new System.EventHandler(this.btn_test_Click);
    this.Load += new System.EventHandler(this.Page_Load); }
    #endregion private void btn_test_Click(object sender, System.EventArgs e)
    {
    TextBox oTxb = new TextBox();
    for (int i=0;i<Page.Controls[1].Controls.Count;i++)
    {
    if (Page.Controls[1].Controls[i].GetType() == oTxb.GetType())
    {
    oTxb = (TextBox)Page.Controls[1].Controls[i]; Response.Write("第" + i.ToString() +"个值为" + oTxb.Text + "<br>");
    }
    }
    }
    }
    }-------------------------------------------------------------------------------
      

  2.   

    你在Csdn上随便搜搜,一定能找到你的问题的答案的
    我记得这个问题曾经有很多人问过!!
      

  3.   

    Function AddRow(ByVal i, ByVal tr1)        Dim td1 As New TableCell()
            Dim td2 As New TableCell()
            Table2.CellPadding = 0
            Table2.CellSpacing = 0        tr1.BackColor = Color.LightGray        td1.HorizontalAlign = HorizontalAlign.Center
            td2.HorizontalAlign = HorizontalAlign.Center
            td1.VerticalAlign = VerticalAlign.Middle
            td1.BorderColor = Color.Black
            td2.BorderColor = Color.Black        td1.BorderWidth = Unit.Pixel(1)
            td2.BorderWidth = Unit.Pixel(1)        Dim txtXx As New TextBox()
            txtXx.Width = Unit.Pixel(50)
            txtXx.ID = "xx" + i.ToString()
            td1.Text = "option" + i.ToString()
            td2.Controls.Add(txtXx)        tr1.Cells.Add(td1)
            tr1.Cells.Add(td2)        Table2.Rows.Add(tr1)    End Function
    每行一个textbox 用行号来区别取值
      

  4.   

    protected void CreateBox() 
    {
    DataTable dt = (DataTable)Session["adt"];
    for(int i=1;i<=dt.Rows.Count;++i)
    {

    TableRow tr = new TableRow();
    TableCell td1 = new TableCell();
    TableCell td2 = new TableCell();
    td2.ID = "mytd"+i.ToString();
    td1.Text = dt.Rows[i-1].ItemArray[1].ToString();
    td1.BorderColor = Color.FromArgb(144,144,144);
    td1.BackColor = Color.FromArgb(227,243,255);
    td1.Height = Unit.Pixel(25);

    TextBox box = new TextBox();
    box.ID = "TextBox"+i.ToString();
    box.Attributes.Add("runat","server");
    td2.Controls.Add(box); tr.Cells.Add(td1);
    tr.Cells.Add(td2);
    Table4.Rows.Add(tr);
    }
    }
    形成一个表
    为什么string myvalue = ((TextBox)Page.FindControl("TextBox"+i.ToString())).Text;
    取不到值?出现未将对象引用到对象实例
      

  5.   

    原理:
    页面上放一个容器控件,写代码,把TEXTBOX加到容器的CHILD就可以了,可以加多个