想单击Button1动态生成控件,然后单击Button2读出刚才生成的控件的值,但是出错了未将对象引用设置到对象的实例。 
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。源错误: 
行 25:     protected void Button2_Click(object sender, EventArgs e)
行 26:     {
行 27:         Response.Write(aa.Text);
行 28:     }
行 29: }
 
cs代码:using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;public partial class _Default : System.Web.UI.Page
{
    public TextBox aa;
    protected void Page_Load(object sender, EventArgs e)
    {
        
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        aa = new TextBox();
        aa.Text = "aa的值";
        aa.ID = "aid";
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        Response.Write(aa.Text);
    }
}

解决方案 »

  1.   

    这个可以在Page_Load的时候得到。
      

  2.   

    问题是我需要能自己控制控件何时生成,因为以后还要对其进行扩充的,不能让它每次Page_Load都生成。
      

  3.   

    说实在的给这类问题回帖会累死的,只能挑着回帖。这是一个最基础的原理——web页面本质上是无状态,因此当点击了Button2的时候,服务器端必须“重建”所有控件。为什么那些写在设计器上的控件(Button1、Button2)不需要重建,因为asp.net读取aspx文件编译的时候给你翻译成了代码、它每次在页面的Init事件之前都已经重建了所有aspx上声明的控件。
    public partial class _Default : System.Web.UI.Page
    {
        public TextBox aa;
        protected void Page_Load(object sender, EventArgs e)
        {
            if(ViewState["created"] != null)
            {
               aa=new TextBox();
               aa.ID = "aid";
               this.Form.Controls.Add(aa);
            }
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            if(aa!=null)
            {
              aa = new TextBox();
              aa.Text = "aa的值";
              aa.ID = "aid";
              this.Form.Controls.Add(aa);
              ViewState["created"]=true;  //记录下标志说明此TextBox已经创建
            }
        }
        protected void Button2_Click(object sender, EventArgs e)
        {
            if(aa!=null)
                Response.Write(aa.Text);  //我不赞成这样写,主张用 RegisterStartupScript方法
        }
    }
      

  4.   

    放心,Button2点击的时候输出的是客户端修改并提交的新值。只要你在page_load中创建,asp.net就会在page_load结束后紧接着立刻回填客户端post上来的值。(这是asp.net2.0的逻辑,而asp.net1.1是在this.Form.Controls.Add(aa);这条语句中就立刻回填值),因此在page_load之后的事件处理方法Button2_Click中aa已经得到的是客户端提交的值。
      

  5.   

    楼上正解,加个容器可用于显示:
    protected System.Web.UI.WebControls.Button Button1;
    protected System.Web.UI.WebControls.Button Button2;
    protected System.Web.UI.WebControls.PlaceHolder PlaceHolder1;
    public TextBox aa;

    private void Page_Load(object sender, System.EventArgs e)
    {
    // 在此处放置用户代码以初始化页面
    if(ViewState["created"] != null)
    {
    aa=new TextBox();
    aa.ID = "aid";
    this.PlaceHolder1.Controls.Add(aa);
    }
    } #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 private void Button1_Click(object sender, System.EventArgs e)
    {
    if(ViewState["created"] == null)
    {
    aa = new TextBox();
    aa.Text = "aa的值";
    aa.ID = "aid";
    this.PlaceHolder1.Controls.Add(aa);
    ViewState["created"]=true;  
    }
    }
    private void Button2_Click(object sender, System.EventArgs e)
    {
    if(ViewState["created"] != null)
    Response.Write(aa.Text);
    }
      

  6.   

    protected void Button1_Click(object sender, EventArgs e)
        {
            if(aa==null)  //原回帖写 a!=null 是错的
      

  7.   

    leahb() ( ) 信誉:100    Blog  2006-10-22 0:14:30  得分: 0  
     
     
       
    你们的代码也太繁了吧
    2.0 里有更简单的方法
      asp.net2.0里真的有更好的方法吗?
     
      

  8.   

    Button2 换成HTML控件这事就很好解决了!
      

  9.   

    50277(柳影随风) ( ) 信誉:100    Blog  2006-10-24 09:47:00  得分: 0  
     
     
       Button2 换成HTML控件这事就很好解决了!
      
     
    换成HTML控件那怎么执行server的代码读取aa的Text?
      

  10.   

    其实这是很正常的,因为WEB开发是无状态的,也就是说上次事件的操作在此次操作是是无效,所以说在PageLoad加载是不要用if(!Page.IsPostBack)判断,因为动态生成的控件是没有状态的.