我创建了一个用户控件USER1
里面包含一个标签和一个按钮,
按钮按下则LABLE1.TEXT="3333";
在一页面动态创建该控件
         Control  ddd = this.LoadControl ("user1.ascx");
        this.Panel1.Controls.Add(ddd);
界面上显示该控件,但是按下控件里的按钮后
整个控件就消失了,并且没有执行LABLE1.TEXT="3333";这句代码
问如何能够使用控件中的代码

解决方案 »

  1.   

    1.直接拖到页面上2.如果要像上面的那样用动态生成,那就应该去掉
    这之前的if(!this.IsPostBack)
      

  2.   

    不是,就是不能加 if(!this.IsPostBack) 这个条件
      

  3.   

    protected void Page_Load(object sender, EventArgs e)
        {    }
        protected void Button1_Click(object sender, EventArgs e)
        {         Control  ddd = this.LoadControl ("user1.ascx");
            this.Panel1.Controls.Add(ddd);
        }
    我的代码是这样的
      

  4.   

    你的代码在下次Page_load的时候没有执行添加,也就是说每次要添加.
      

  5.   

    protected void Page_Load(object sender, EventArgs e)
        {
              if(ViewState["A"] == "1")
              {
    this.AddControl();
           }
        }void AddControl()
    {
         Control  ddd = this.LoadControl ("user1.ascx");
            this.Panel1.Controls.Add(ddd);
         ViewState["a"] = "1";
    }protected void Button1_Click(object sender, EventArgs e)
        {         this.AddControl();
        }
      

  6.   

    lz可以自己写用户控件的属性,如写个DataSource的属性来接受数据然后在属性里进行处理
      

  7.   

    由于将用户控件类型转变为Control后,写在控件里的属性都没有了
      

  8.   

    用你的用户控件强制类型转换Control就可以了