Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6049969
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:38:10+00:00 2026-05-23T07:38:10+00:00

Consider a web form (aspx) wth an associated codefile (aspx.cs). In the codefile, for

  • 0

Consider a web form (aspx) wth an associated codefile (aspx.cs).

In the codefile, for instance, we have the following:

public partial class MyPage : System.Web.UI.Page {

   int myint;
   string mystring;

   public void Page_Load(object sender, EventArgs e) {
      ...
      if (!this.IsPostBack) {
         this.myint = 2;
         this.mystring = "Hello";
      }
      ...
   }

}

Can I expect to have the two variables everytime the page is posted back? I mean: are page class fields kept as part of the web form state?

Thankyou

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T07:38:10+00:00Added an answer on May 23, 2026 at 7:38 am

    No, these objects would be disposed at the end of the Page-Lifecycle.
    If you want to persist them in ViewState you have to add them to it.

    Objects that need to be stored in ViewState must be serializable.

    Storing Simple Data Types in the ViewState

    Like most types of state management in
    ASP.NET, view state relies on a
    dictionary collection, where each item
    is indexed with a unique string name.
    For example, consider this code:

    ViewState["ViewStateVariableName"] = 1;
    

    This places the value 1 (or rather, an
    integer that contains the value 1)
    into the ViewState collection and
    gives it the descriptive name
    ViewStateVariable. If there is
    currently no item with the name
    ViewStateVariable, a new item will be
    added automatically. If there is
    already an item indexed under this
    name, it will be replaced.

    You can access this variable anywhere
    within the page/control where the
    viewstate variable has been added.
    When retrieving a value, you use the
    key name.

    int number = (int) ViewState["ViewStateVariable"];
    

    You also need to cast the retrieved
    value to the appropriate data type.
    This is because the ViewState
    collection stores all items as generic
    objects which also give you the
    flexibility to store any type of basic
    data types in it. In fact, you can
    even store your custom objects in the
    view state.

    Storing Objects in View State

    You can store your own objects in view
    state just as easily as you store
    numeric and string types. However, to
    store an item in view state, ASP.NET
    must be able to convert it into a
    stream of bytes so that it can be
    added to the hidden input field in the
    page. This process is called
    serialization. If your objects aren’t
    serializable (and by default they
    aren’t), you’ll receive an error
    message when you attempt to place them
    in view state.

    • http://www.beansoftware.com/ASP.NET-Tutorials/ViewState-In-ASP.NET.aspx
    • http://msdn.microsoft.com/en-us/magazine/cc188774.aspx
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following .Net ASMX web service with two web methods. using System; using
What web development frameworks natively support JasperReports? Consider the following form as an example:
Consider a web form with a dropdown. Based on the selection in the dropdown,
Consider two web pages with the following in their body respectively: <body> <script> document.writeln('<textarea></textarea>')
Consider the following code: index.html <!DOCTYPE HTML PUBLIC -//W3C//DTD HTML 4.01//EN http://www.w3.org/TR/html4/strict.dtd> <html> <head>
This question is about a very strange behavior of web controls. Consider the following
Lets consider next scenario: assume I have a web app, and authentication of users
Please consider the following scenario, There are two web applications App1 & App2. A
I'm developing a PHP-based web-application in which you have a form with textarea inputs
I have a System.Windows.Form and a IntPtr acting as HWND. I want each of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.