Export (0) Print
Expand All

UserControl.Session Property

Gets the Session object for the current Web request.

[Visual Basic]
Public ReadOnly Property Session As HttpSessionState
[C#]
public HttpSessionState Session {get;}
[C++]
public: __property HttpSessionState* get_Session();
[JScript]
public function get Session() : HttpSessionState;

Property Value

An HttpSessionState object associated with the Page that contains the UserControl instance.

Example

[Visual Basic, C#] The following example adds values to the Session property of a user control. The myControl.Session.Add syntax inserts the values of two TextBox Web server controls to the session associated with the user control and the page that contains it.

[Visual Basic] 
' Saves state information which is used by display handler after the postback has occured.
    
Sub SubmitBtn_Click(Sender As Object, e As EventArgs) 
    
    ' 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
End Sub

Sub Display_Click(Sender As Object,e As EventArgs)
    
      Dim position As Integer = Session.Count - 1
    ' Extract stored UserControl from the session state of page.
      Dim tempControl As LogOnControl = CType(Session(Session.Keys(position)),LogOnControl)
    ' 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
End Sub

[C#] 
// Save state information which is used by display handler after the postback has occured.
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;
}

[C++, JScript] No example is available for C++ or JScript. To view a Visual Basic or C# example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 2000, Windows XP Professional, Windows Server 2003 family

See Also

UserControl Class | UserControl Members | System.Web.UI Namespace | Page.Session | HttpSessionState

Show:
© 2014 Microsoft