后台手动声明:protected User UserName = new User(); //不能这样声明。用户控件不用在后后进行实例化。
就像你声明一个button或者其它 的控件。
这个控件的名字要和页面声明的名字一样。 页面框架就自动帮你实例化这个控件。。
比如:
前台:
<asp:Label id="lblSpecial" runat="server"></asp:Label>
后台:
protected System.Web.UI.WebControls.Label lblSpecial;

解决方案 »

  1.   

    protected User UserName; 即可
      

  2.   

    将“protected User UserName = new User();”改为“protected User UserName;”方法public string GetString()可以不要,直接使用UserName.Label1.Text取得该控件的值
      

  3.   

    protected User UserName ; UserName 即被实例化
      

  4.   

    我去掉了new部分,但是还是提示“未将对象引用设置到对象的实例”,大家帮忙看看是不是还有其他的问题。行 50:  private void Button1_Click(object sender, System.EventArgs e)
    行 51:  {
    行 52:  string tt = UserName.Label112.Text;源文件: c:\inetpub\wwwroot\test_webcontrol\test.aspx.cs    行: 52 
      

  5.   

    string tt = UserName.Label112.Text;==>你的ascx的界面上有没有 Label112??
      

  6.   

    UserName.Label1.Text
    这样就可以了,那个方法可以不要
      

  7.   

    public string GetString()方法实际上是进行了一个很复杂的处理,我在做测试代码是,将这个简化了,这个方法是要的。
    即使不要,直接用UserName.Lable1.Text也是会出错,报未将对象引用到对象的实例
      

  8.   

    private void Button1_Click(object sender, System.EventArgs e)
    行 51:  {
    行 52:  string tt = UserName.Label112.Text;
                                                      ~~~~~~~~
    Label1
      

  9.   

    解决了!多谢大家。
    证人:大家仔细看我的代码,我是拖动控件到test.aspx页面的的,默认的id是User1,而我在test.aspx.cs中声明的确实UserName,当然没有实例了。
    (微软这点做的不好,既然是拖动产生的用户控件,后台就应该也声明一下,还得让人手动声明,是容易出错)