This documentation is archived and is not being maintained.

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 HttpSessionState Session { get; }

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.


// 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;
}


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Show: