DNR
DNR

Reputation: 3746

Calling a session variable in a class object in asp.net

How can I access the values stored in a session object with a class in asp.net?

Upvotes: 0

Views: 5204

Answers (3)

user842818
user842818

Reputation: 335

Have your class constructor take in a HttpContext.Current object parameter. From that object, access the session property.

Upvotes: 0

James Johnson
James Johnson

Reputation: 46077

You should be able to use HttpContext for this:

string sessionValue = HttpContext.Current.Session["SessionKey"] as string;

Just make sure your class imports the System.Web namespace, and this should work fine.

Upvotes: 3

Srikar Doddi
Srikar Doddi

Reputation: 15609

Protected Sub EnterInfoButton_OnClick(ByVal sender As Object, ByVal args As EventArgs)
    Session("FirstName") = Server.HtmlEncode(FirstNameTextBox.Text)
    Session("LastName") = Server.HtmlEncode(LastNameTextBox.Text)
    Session("Address") = Server.HtmlEncode(AddressTextBox.Text)
    Session("City") = Server.HtmlEncode(CityTextBox.Text)
    Session("StateOrProvince") = Server.HtmlEncode(StateOrProvinceTextBox.Text)
    Session("ZipCode") = Server.HtmlEncode(ZipCodeTextBox.Text)
    Session("Country") = Server.HtmlEncode(CountryTextBox.Text)

    EnterUserInfoPanel.Visible = False
    UserInfoPanel.Visible = True

    SetLabels()
  End Sub

Upvotes: 0

Related Questions