0

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

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

Comments

0
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

1 Comment

@James Johnson, Within a class that inherits from System.Web.UI.Page it will.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.