在页面的button_Click方法中,调用用户控件中的方法。测试过,在用户控件的方法中,在读取一个Session值时就出现异常,而在页面的button_Click中就能够读取到。这种现象可能是什么原因?

解决方案 »

  1.   

    可以用Session的,看看是不是其他地方的错
      

  2.   

    void SubmitBtn_Click(Object sender, EventArgs e) 
    {
        // Clear all values from session state of 'Page'.
        Session.Clear();
        // Populate Session State of UserControl with the values entered by user.
        myControl.Session.Add("username",myControl.user.Text);
        myControl.Session.Add("password",myControl.password.Text);
        // Add UserControl state to the SessionState object of Page.
        Session[myControl.user.Text]= myControl;
        display.Enabled = true;
    }void Display_Click(Object sender, EventArgs e)
    {
        int position = Session.Count - 1;
        // Extract stored UserControl from the session state of page.
        LogOnControl tempControl = (LogOnControl)Session[Session.Keys[position]];
        // Use SessionState of UserControl to display previously typed information.
        txtSession.Text = "<br><br>User:" + tempControl.Session["username"] + "<br>Password : " + tempControl.Session["password"];
        display.Enabled = false;
    }
      

  3.   

    Session放置的地方可能有错,可能还没得到值就已经Session了。
      

  4.   

    建议你作一个控件基类,如ControlBase,里面有一个属性是Session就可以了,可以参考vs.net2003的duwamish
      

  5.   

    Session[myControl.user.Text]= myControl;
    有问题,应该是 Session["username"]= myControl;