Web自定义控件中一个LinkButton LB_Login(其Text属性为"登陆"),一个LinkButton LB_Exit(其Text属性为"退出"),一个Label lbl_Message(其Text属性为"欢迎您").要求刚开始时只显示LB_Login,点击LB_Login后只显示LB_Exit与lbl_Message,然后点击LB_Exit只显示LB_Login.我的问题是:
LB_Login要点击两次才出现LB_Exit与lbl_Message;LB_Exit要点击两次才出现LB_Login,代码如下,高手指点一下这是什么原因?
public class WebControl_Test:System.Web.UI.WebControls.WebControl,INamingContainer
{
    protected override void CreatChildControls()
    {
         LinkButton LB_Login;
         LB_Login=new LinkButton();
         LB_Login.Text="登陆";
         LB_Login.Click+=new EventHandler(this.LB_Login_Click);         LinkButton LB_Exit;
         LB_Exit=new LinkButton();
         LB_Exit.Text="登陆";
         LB_Exit.Click+=new EventHandler(this.LB_Exit_Click);         Label lbl_Message;
         lbl_Message=new Label();
         lbl_Message.Text="欢迎您";         if((string)Context.Session["currentUser"]=="")
         {Controls.Add(LB_Login);}
         else
         {Controls.Add(lbl_Message);Controls.Add(LB_Exit);}         
     }
     protected void LB_Login_Click(Object sender,System.EventArgs e)
     {Context.Session["currentUser"]=="aa";}
     protected void LB_Exit_Click(Object sender,System.EventArgs e)
     {Context.Session["currentUser"]=="";}
}高手帮帮忙,谢谢!!!

解决方案 »

  1.   

    tryWebControl_Test:System.Web.UI.WebControls.WebControl,INamingContainer
    {
        protected override void CreatChildControls()
        {
             LinkButton LB_Login;
             LB_Login=new LinkButton();
             LB_Login.Text="登陆";
             LB_Login.Click+=new EventHandler(this.LB_Login_Click);         LinkButton LB_Exit;
             LB_Exit=new LinkButton();
             LB_Exit.Text="登陆";
             LB_Exit.Click+=new EventHandler(this.LB_Exit_Click);         Label lbl_Message;
             lbl_Message=new Label();
             lbl_Message.Text="欢迎您";          Controls.Add( LB_Login);
     Controls.Add( LB_Exit);
     Controls.Add(lbl_Message);  
         }     void ShowLoginInfo(bool bShow)
         {
             LB_Login.Visible = bShow;
            LB_Exit.Visible = !bShow;
            lbl_Message.Visible = !bShow;
         }
      protected override void OnPreRender(EventArgs e)
      {
            if((string)Context.Session["currentUser"]=="")
             {ShowLoginInfo(true);}
             else
             {ShowLoginInfo(false);}     
      }
      

  2.   

    自己没写过usercontrol,学习中
      

  3.   

    saucer(思归)大哥:谢谢!!!
    但我不明白为什么要将Context.Session["currentUser"]的判断写在OnPreRender中,而不能写在CreatChildControls中呢?
      

  4.   

    because CreateControls is called before your event handler, after you change the value in the Session variable, the effect will not be applied until next postback
      

  5.   

    saucer(思归)大哥:那么OnPreRender发生在CreateControls之后,event handler之前吗?
      

  6.   

    OnPreRender发生在postback event handler之后, SaveViewState/Render之前
      

  7.   

    即然你说CreateControls is called before your event handler,那么因为OnPreRender发生在CreateControls之前,所以OnPreRender发生在event handler之前了?
      

  8.   

    在这些方法里用Page.Response.Write输出些文字,你就知道哪个先哪个后了
      

  9.   

    saucer(思归)大哥:我是用如下代码测试的.但每次只输出
    CreateChildControlsOnPreRender 
       退出登陆 
    并且退出与登陆按钮点击后没有输出Login_Click或Exit_Click,这是为什么?
    测试说明CreateChildControls在OnPreRender之前发生,但按钮事件就不知道了!public class WebCustomControl_Test : System.Web.UI.WebControls.WebControl
    {
      protected override void OnPreRender(System.EventArgs e)
      {
        Page.Response.Write("OnPreRender");
      }
      protected override void CreateChildControls()
      {
        LinkButton LB_Login;
        LinkButton LB_Exit;    LB_Login=new LinkButton();
        LB_Login.Text = "登陆";
        LB_Login.Click+=new EventHandler(this.LB_Login_Click);    LB_Exit=new LinkButton();
        LB_Exit.Text = "退出";
        LB_Exit.Click+=new EventHandler(this.LB_Exit_Click);    Controls.Add(LB_Exit);
        Controls.Add(LB_Login);    Page.Response.Write("CreateChildControls");
      }
      protected void LB_Login_Click(object sender, System.EventArgs e)
      {
        Page.Response.Write("Login_Click");
      }
      protected void LB_Exit_Click(object sender, System.EventArgs e)
      {
        Page.Response.Write("Exit_Click");
      }
      

  10.   

    implement INamingContainer,give controls IDspublic class WebCustomControl_Test : System.Web.UI.WebControls.WebControl,INamingContainer...LinkButton LB_Login;
        LinkButton LB_Exit;    LB_Login=new LinkButton();
        LB_Login.Text = "登陆";
        LB_Login.ID = "login";
        LB_Login.Click+=new EventHandler(this.LB_Login_Click);    LB_Exit=new LinkButton();
        LB_Exit.Text = "退出";
        LB_Exit.ID = "exit";
        LB_Exit.Click+=new EventHandler(this.LB_Exit_Click);
    ....
      

  11.   

    忘了加INamingContainer,
    我测试结果为如下顺序CreateChildControls Login_Click OnPreRender我真不明白OnPreRender为什么在CreateChildControls之后。CreateChildControls与Render是什么关系呢?
      

  12.   

    请读一下
    Page Events: Order and PostBack
    http://aspalliance.com/articleViewer.aspx?aId=134&pId=
      

  13.   

    saucer(思归)大哥:受益匪浅,谢谢!!!
      

  14.   

    调试一下,看看执行的顺序
    先执行CreatChildControls()
    然后执行LB_Login_Click()等
    不会再执行CreatChildControls()了