[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 211
  • Last Modified:

Variables and ASP.NET

Hi all!!!

    I need to add a new user to my ASP.NET application. This process is done filling three Forms (UserControls). When i validate the first form, I create a new object CadUser, that stores all user information and roles that is being filled by the forms.

    Suppose that i ended filling my first form. So, i click on my NextStep button. When I Click on it, a new CadUser object is created (It is created as as "Class Level" object, not as "Method Level") and i redirected to the 2nd form. So, i fill the 2nd form. When i will store the information filled on the 2nd form, i try to get a reference to the CadUser obj created before, and it is not there... (CadUser = Nothing)

    So, i need to know how i could store objects that will be read after a page postback and how could i dispose the resources used by this object when i don't need to use it anymore.

1 Solution
ASP.NET pages are stateless so you need to use one of the methods of maintaining state: Session, ViewState, Cache. Session is specific to your user and lasts across pages -- it is disposed of when the user's Session ends (i.e. they close the browser, etc.), ViewState is specific to your user and lasts for the life of a page (when the user leaves the one page the ViewState is gone) and is stored in a hidden field in your page code which increases the page's download size, Cache is global to your application (so if user1 creates an object with cache name "NewUser" and user 2 creates an object with cache name "NewUser", user 1's object is gone and he will access user 2's object) and is disposed of based upon an expiration date you set or you can manually dispose of cached objects.

If you're using a lightweight class (like a user with just a name/basic information in it) I would use Session to store the information between pages or ViewState if you're only dealing with one page and postbacks.


Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now