Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

UserControl.Session Property

Gets the HttpSessionState object for the current Web request.

Namespace:  System.Web.UI
Assembly:  System.Web (in System.Web.dll)

<BrowsableAttribute(False)> _
Public ReadOnly Property Session As HttpSessionState

Property Value

Type: System.Web.SessionState.HttpSessionState
An HttpSessionState object associated with the Page that contains the UserControl instance.

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.

' 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'.

    ' Populate Session State of UserControl with the values entered by user.

    ' 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

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1
© 2015 Microsoft