protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Label lab = new Label();
            lab.ID = "lab1";
            lab.Text = "11111";
            Panel1.Controls.Add(lab);
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        TextBox1.Text = ((TextBox)(Panel1.FindControl("lab1"))).Text;//未将对象引用到实例
    }

解决方案 »

  1.   

    protected void Page_Load(object sender, EventArgs e)
        {
           
                Label lab = new Label();
                lab.ID = "lab1";
                lab.Text = "11111";
                Panel1.Controls.Add(lab);
            
        }
        protected void Button1_Click(object sender, EventArgs e)
        {        TextBox1.Text = ((Label)Panel1.FindControl("lab1")).Text;//未将对象引用到实例 
        }
      

  2.   

    页面上可以看到加上的LABLE
    可是就是获取不到它的值。
      

  3.   

    那是加载的时候。你用了!IsPostBack要不把前面得!
      

  4.   

    楼上已经说了,如果想获得,把代码放在 if (!IsPostBack) 之外
      

  5.   

      protected void Button1_Click(object sender, EventArgs e)
        {
            Label lab = new Label();
            lab.ID = "lab1";
            lab.Text = "11111";
            Panel1.Controls.Add(lab);
            TextBox1.Text = lab.Text;
        }
      

  6.   

    为什么我的可以
    你用我的嗲吗测试下呢
    前台
     <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
            <asp:Panel ID="Panel1" runat="server" Height="50px" Width="125px">
            </asp:Panel>
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    后台
     protected void Page_Load(object sender, EventArgs e)
        {
           
                Label lab = new Label();
                lab.ID = "lab1";
                lab.Text = "11111";
                Panel1.Controls.Add(lab);
            
        }
        protected void Button1_Click(object sender, EventArgs e)
        {        TextBox1.Text = ((Label)Panel1.FindControl("lab1")).Text;//未将对象引用到实例 
        }
      

  7.   

    LZ
    没有理解
    Page.ISPostBack()的用法