for (int i=1; i<=numtexts; i++) 
{
TextBox t = new TextBox();
t.Text = "TextBox" + (i).ToString();
t.ID = "TextBox" + (i).ToString();
Panel1.Controls.Add(t);
Panel1.Controls.Add(new LiteralControl("<br>"));

 }
这是用panel面版装载textbox控件,但是我不能
用response.write输出textbox的植,也就是前台web输入值后
感觉这里的应该response.write(“ 'TextBox' + (i).ToString()+'.Text'”)但是不行,好象就是当字符串输出文本 ,但是如果textbox不放在panel面版里那么可以用response.write(textbox的id.text)
输出,但是,如果不放在panel里又不能动态输出服务器控件!!请各位务必帮忙!谢谢!

解决方案 »

  1.   

    for (int i=0;i<Panel1.Controls.Count;i++)
    {
    Control ctl = Panel1.FindControl("TextBox" + i.ToString());if (ctl is TextBox)
    {
    Response.Write();
    }
    }
      

  2.   

    其实就是这么一回事!按“按钮1”我动态生成几个textbox然后通过web输入值,
    然后再按“按钮2”,后台把刚输入的几个textbox值连接并输出。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 TestControls
    {
    /// <summary>
    /// 
    /// </summary>
    public class WebForm1: System.Web.UI.Page
    {
    protected System.Web.UI.WebControls.Panel Panel1;
    protected System.Web.UI.WebControls.Button Button1;
    protected System.Web.UI.WebControls.CheckBox Check1;
    protected System.Web.UI.WebControls.Button Button2;
            protected System.Web.UI.WebControls.DropDownList DropDown2;

    private void Page_Load(object sender, System.EventArgs e)
    {
    } #region Web 窗体设计器生成的代码
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
    //
    InitializeComponent();
    base.OnInit(e);
    }

    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {    
    this.Button1.Click += new System.EventHandler(this.Button1_Click);
    this.Button2.Click += new System.EventHandler(this.Button2_Click);
    this.Load += new System.EventHandler(this.Page_Load); }
    #endregion public void  Button1_Click(object sender, System.EventArgs e)
    {
     
                 
    int numtexts = Int32.Parse(DropDown2.SelectedItem.Value);
                 Panel1.Controls.Add(new LiteralControl("<br>"));
    for (int i=1; i<=numtexts; i++) 
    {



    TextBox t = new TextBox();

    t.ID = "TextBox" + (i).ToString();
    Panel1.Controls.Add(t);
    Panel1.Controls.Add(new LiteralControl("<br>"));




    }
                 



    }
    }


    private void Button2_Click(object sender, System.EventArgs e)
    {

    int numtexts = Int32.Parse(DropDown2.SelectedItem.Value);
    string aaa="";
                    for (int i=1; i<=numtexts; i++) 
    {   



    TextBox tb=(TextBox)Panel1.FindControl("TextBox1" + (i).ToString());

    Response.Write(tb.Text);//这两句报错!                        aaa=aaa+tb.Text ;     //                                   
      }Object reference not set to an instance of an object. 
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.Source Error: 
    Line 115: TextBox tb=(TextBox)Panel1.FindControl("TextBox1" + (i).ToString());
    Line 116:
    Line 117: Response.Write(tb.Text);
    Line 118:                





    }
    }
      

  3.   

    Source Error: 
    Line 115: TextBox tb=(TextBox)Panel1.FindControl("TextBox1" + (i).ToString());
    Line 116:
    Line 117: Response.Write(tb.Text);
    Line 118:               
    ============================================================
    TextBox tb=(TextBox)Panel1.FindControl("TextBox1" + (i).ToString());
    -->
    TextBox tb=(TextBox)Panel1.FindControl("TextBox" + (i).ToString());你太粗心了!
      

  4.   

    老兄!还是不行啊!改后还是那个错!
    Line 117: Response.Write(tb.Text);
    Exception Details: System.NullReferenceException: Object reference not set to an instance of an object
      

  5.   

    由于页面的刷新导致你动态创建的控件又被清除了,因此你应加入以下代码:private void Page_Load(object sender, System.EventArgs e)
    {
        if (ViewState["AddTextBox"] != null)
        {
            Button1_Click(null, null);
        }
    }public void  Button1_Click(object sender, System.EventArgs e)
    {
        ... ...    ViewState["AddTextBox"] = true;
    }