想单击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);
}
}
说明: 执行当前 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);
}
}
解决方案 »
- 。net 怎么调用迅雷
- asp.net 怎么判断 我的网页被 设为了首页
- 个人要开个人才网,需要做些什么准备呢
- 为什么flash调用net中的一个js引用不能执行js中的方法?
- 如何用asp.net 实现注册时邮件击活功能啊?请好心大侠“南慕白(兄),北晓峰(兄)”看看啊!
- Repeater嵌套,CheckBox选择问题,请帮助
- 除了梅花系列日期无刷新控件外还有什么好控件吗?(梅花不好用啊!)
- 关于asp.net的问题
- 如何控制浮点型数据的长度
- 如何实现下拉框的值随选择的变化而变化???
- 新闻发布系统,中大类和小类都定死的,新闻和小类关联,我后台和前台怎么把小类和文章对应起来
- 急!!TreeView与iframe问题
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方法
}
}
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);
}
{
if(aa==null) //原回帖写 a!=null 是错的
你们的代码也太繁了吧
2.0 里有更简单的方法
asp.net2.0里真的有更好的方法吗?
Button2 换成HTML控件这事就很好解决了!
换成HTML控件那怎么执行server的代码读取aa的Text?