用户控件:test.ascx
环境:vs2003
出现问题:
         行 25:  return this.Label1.Text;
         未将对象引用设置到对象的实例
代码:
using System;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls; /// <summary>
/// test 的摘要说明。
/// </summary>
public class test : System.Web.UI.UserControl
{
protected System.Web.UI.WebControls.Label Label1; private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
}
public string brand
{
get
{
return this.Label1.Text;
}
} #region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器
/// 修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load); }
#endregion问题不大,我在写其他的控件时出现了这个问题,直接写成这样的一个东西,依旧出现这样的提示。各位如果遇到类似问题请指点一下。

解决方案 »

  1.   

    我在前台页面声明的控件:
              test test1;
    test1=new test();
    提取值:
    lblmsg.Text=test1.brand;应该说我只加入一个lable,提取返回值应该是没有问题的
      

  2.   

    大家看看把我这个代码贴回去看看能否运行:
    webform1.aspx
    <%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="test.WebForm1" %>
    <%@ Register TagPrefix="uc1" TagName="test" Src="test.ascx" %>
    <!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">
    <FONT face="宋体">
    <asp:Label id="lblmsg" style="Z-INDEX: 101; LEFT: 104px; POSITION: absolute; TOP: 88px" runat="server">Label</asp:Label>
    <asp:Button id="Button1" style="Z-INDEX: 102; LEFT: 344px; POSITION: absolute; TOP: 160px" runat="server"
    Text="Button"></asp:Button>
    <uc1:test id="test1" runat="server"></uc1:test></FONT>
    </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 test
    {
    /// <summary>
    /// WebForm1 的摘要说明。
    /// </summary>
    public class WebForm1 : System.Web.UI.Page
    {
    protected System.Web.UI.WebControls.Button Button1;
    protected System.Web.UI.WebControls.Label lblmsg;
    // test test1; private void Page_Load(object sender, System.EventArgs e)
    {
    // test1=new test();
    test test1=(test)Page.FindControl("test1");
    } #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.Load += new System.EventHandler(this.Page_Load); }
    #endregion private void Button1_Click(object sender, System.EventArgs e)
    {
    lblmsg.Text=test1.brand;
    }
    }
    }test.ascx
    <%@ Control Language="c#" AutoEventWireup="false" Codebehind="test.ascx.cs" Inherits="test.test" TargetSchema="http://schemas.microsoft.com/intellisense/ie5"%>
    <asp:Label id="Label1" runat="server">Label</asp:Label>test.ascx.cs
    namespace test
    {
    using System;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls; /// <summary>
    /// test 的摘要说明。
    /// </summary>
    public class test : System.Web.UI.UserControl
    {
    protected System.Web.UI.WebControls.Label Label1; private void Page_Load(object sender, System.EventArgs e)
    {
    this.Label1.Text="tesst";
    // 在此处放置用户代码以初始化页面
    }
    public string brand
    {
    get
    {
    return Label1.Text;
    }
    } #region Web 窗体设计器生成的代码
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
    //
    InitializeComponent();
    base.OnInit(e);
    }

    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器
    /// 修改此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
    this.Load += new System.EventHandler(this.Page_Load); }
    #endregion
    }
    }
      

  3.   

    只要在HTML加上<asp:Label id="Label1"  Text='<%#brand%>'>就可以拉
      

  4.   

    我尝试采用return "test"返回,提示返回成功.
    为什么只要采用控件的返回值就提示实例错误?搞不明白
      

  5.   

    问题解决:
    采用如下命名,
    test1  = (test)FindControl("test1");
    现在问题是,test1=new test()的命名为什么不对?
    欢迎大家参与讨论