我的ascx文件代码如下:
public class WebUserControl1 : System.Web.UI.UserControl
{
protected System.Web.UI.WebControls.TextBox t1;
public string t1_text{

get{
return this.t1.Text.ToString().Trim();
}
set{
this.t1.Text=value;
}
}
         }
aspx文件代码如下:
public class index : System.Web.UI.Page
{
private WebUserControl1 write=new WebUserControl1();//突破口
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页
if(!Page.IsPostBack)
{ write.t1_text="wooawhdashdoahsdosahdsaodhsoh";
}
}
          }
出现的问题如下:
“/st”应用程序中的服务器错误。
--------------------------------------------------------------------------------未将对象引用设置到对象的实例。 
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。源错误: 
行 38:  }
行 39:  set{
行 40:  this.t1.Text=value;经本人调试知道
程序将值给了value但是this.t1.Text=value;处出错!!!!

解决方案 »

  1.   

    this.t1.Text???
    不应该是this.Text吗?
      

  2.   

    GG啊
    怎么会这样呢
    this.t1.Text=t1.Text
      

  3.   

    看错了,t1_text应该是WebUserControl1的属性啊,所以程序执行到this.t1.Text处报错
      

  4.   

    GG啊
    出错处是在用户控件里面及ascx里面怎么会和aspx页面有联系
      

  5.   

    private WebUserControl1 write=new WebUserControl1();//突破口
    改为:
    private WebUserControl1 write;
    write是页面中用户控件的id
      

  6.   

    客户端aspx是否拖拽用户控件到上面,<%@ Register TagPrefix="uc1" TagName="Control1" Src="xxx.ascx" %> 
      

  7.   

    testControl.ascx<%@ Control Language="C#" AutoEventWireup="true" CodeFile="testControl.ascx.cs" Inherits="testControl" %>
    <TABLE id="Table1" style="FONT-SIZE: 9pt; WIDTH: 183px; HEIGHT: 125px" cellSpacing="1"
     cellPadding="1" width="183" align="center" border="1">
     <TR>
      <TD height="20">
       <asp:Label id="test1" runat="server">test1:</asp:Label>
       <asp:TextBox id="t1" Width="128px" runat="server"></asp:TextBox></TD>
     </TR>
     <TR>
      <TD height="20"><FONT face="宋体">
        <asp:Label id="test2" runat="server">test2:</asp:Label>
        <asp:TextBox id="t2" Width="128px" runat="server"></asp:TextBox></FONT></TD>
     </TR>
    </TABLE>testControl.ascx.csusing 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 testControl : System.Web.UI.UserControl
    {
        //protected void Page_Load(object sender, EventArgs e)
        //{    //}    public string t1_text
        {
            get
            {
                return this.t1.Text;
            }
            set
            {
                this.t1.Text = value;
            }
        }    public string t2_text
        {
            get
            {
                return this.t2.Text;
            }
            set
            {
                this.t2.Text = value;
            }
        }}
      

  8.   

    Default.aspx<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %><%@ Register Src="testControl.ascx" TagName="testControl" TagPrefix="uc1" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>无标题页</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <uc1:testControl ID="TestControl1" runat="server" />
            &nbsp;</div>
        </form>
    </body>
    </html>
    Default.aspx.csusing System;
    using System.Data;
    using System.Configuration;
    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 
    {    protected void Page_Load(object sender, EventArgs e)
        {
            TestControl1.t1_text = "test1";
            TestControl1.t2_text = "test2";
        }
    }
      

  9.   

    我用的是VS.net2005不过道理是一样的
      

  10.   

    基本上可以判定为t1 = null,至于t1.Text就更加不存在了。检查你的UserControl的逻辑,t1在什么时候不为null,那之后才能操作t1.Text的值。
      

  11.   

    testControl.ascx<%@ Control Language="C#" AutoEventWireup="true" CodeFile="testControl.ascx.cs" Inherits="testControl" %>
    <TABLE id="Table1" style="FONT-SIZE: 9pt; WIDTH: 183px; HEIGHT: 125px" cellSpacing="1"
     cellPadding="1" width="183" align="center" border="1">
     <TR>
      <TD height="20">
       <asp:Label id="test1" runat="server">test1:</asp:Label>
       <asp:TextBox id="t1" Width="128px" runat="server"></asp:TextBox></TD>
     </TR>
     <TR>
      <TD height="20"><FONT face="宋体">
        <asp:Label id="test2" runat="server">test2:</asp:Label>
        <asp:TextBox id="t2" Width="128px" runat="server"></asp:TextBox></FONT></TD>
     </TR>
    </TABLE>testControl.ascx.csusing 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 testControl : System.Web.UI.UserControl
    {
        //protected void Page_Load(object sender, EventArgs e)
        //{    //}    public string t1_text
        {
            get
            {
                return this.t1.Text;
            }
            set
            {
                this.t1.Text = value;
            }
        }    public string t2_text
        {
            get
            {
                return this.t2.Text;
            }
            set
            {
                this.t2.Text = value;
            }
        }}
      

  12.   

    楼主你试试看行不行,有什么问题给我发email,我的mail:[email protected]
      

  13.   

    //****你引用用户控件错了protected UserControl.Header Header1;//****调用属性
    Header1.Title="我的个人主页";
      

  14.   

    private WebUserControl1 write=new WebUserControl1();//突破口
    --〉private WebUserControl1 write
     也不行
      

  15.   

    private WebUserControl1 write=new WebUserControl1();
    尽管生命了WebUserControl1对象,但此时WebUserControl1并没有初始化到页面,UserControl是一种基于模版控件类型,必须在页面里面添加,否则调用要LoadTemplate("")。
    因为你想要的TextBox此时在模版中,而你根本没有初始化该模版,所以TextBox为null
      

  16.   

    <%@  Control  Language=  "C#  "  AutoEventWireup=  "true  "  CodeFile=  "testControl.ascx.cs  "  Inherits=  "testControl  "  %  > 
    出处的错
    我把AutoEventWireup=  "false "--->"true"
    就行了
    可是不知道为什么   回复人:greennetboy(我的老婆叫静静) 说的好像挺有道理能不能解答一下
      

  17.   

    当AutoEventWireup被设置成为true的时候,在这个页面中的事件处理器是被自动的粘贴到页面事件上边的。