View State Misunderstanding

Discussion in 'ASP .Net' started by Philip Tripp, Jul 24, 2004.

  1. Philip Tripp

    Philip Tripp Guest

    I've read numerous sources stating that view state can be disabled per
    control, and per page, but can't seem to keep web form controls from
    remembering their state on a postback. I'm using VS.Net 2002, .Net
    Framework 1.0 SP2.

    I create a new project, and add a new web form to the project. On the
    page properties, I set EnableViewState to False and verify that the @
    Page directive has "enableViewState=false" in it. I then add a TextBox
    control from the Web Froms, and set its EnableViewState property to
    false. I add a Button control from the Web Forms, and start my project
    (F5). I type in some text to the textbox, click the button so the form
    submits, and expect that the text box come back empty (because view
    state has been disabled), but it always comes back populated.

    I am new to ASP.NET, so am I somehow misunderstanding the dozen or so
    aricles I've read about view state? Is view state always remembered on
    post backs? If it is always remembered, then what the heck does setting
    EnableViewState to false do?
     
    Philip Tripp, Jul 24, 2004
    #1
    1. Advertising

  2. Philip Tripp

    Saber Guest

    what do you mean by "it always comes back populated.
    "
    "Philip Tripp" <> wrote in message
    news:eXll1%...
    > I've read numerous sources stating that view state can be disabled per
    > control, and per page, but can't seem to keep web form controls from
    > remembering their state on a postback. I'm using VS.Net 2002, .Net
    > Framework 1.0 SP2.
    >
    > I create a new project, and add a new web form to the project. On the page
    > properties, I set EnableViewState to False and verify that the @ Page
    > directive has "enableViewState=false" in it. I then add a TextBox control
    > from the Web Froms, and set its EnableViewState property to false. I add a
    > Button control from the Web Forms, and start my project (F5). I type in
    > some text to the textbox, click the button so the form submits, and expect
    > that the text box come back empty (because view state has been disabled),
    > but it always comes back populated.
    >
    > I am new to ASP.NET, so am I somehow misunderstanding the dozen or so
    > aricles I've read about view state? Is view state always remembered on
    > post backs? If it is always remembered, then what the heck does setting
    > EnableViewState to false do?
    >
     
    Saber, Jul 24, 2004
    #2
    1. Advertising

  3. Philip,

    What you are seeing is the expected behavior.

    The ViewState will retain the state of your control if you populate it in
    CodeBehind.

    If you enter data into the control in the browser you sill see this entered
    value in the control in the code behind.

    ViewState will not change the behavior of a text box because after you enter
    text into the textbox control the data is submitted to the server when you
    click the button. Then the server takes the submitted value and places it
    into the text property of the control. When the new page is rendered to the
    broeser the text value is already set and will show up.

    The place that view state will make a lot of difference is with a treeview
    where the data is never submitted back to the server.

    You can check to see if you have any view state by looking at the Source in
    the browser. If there is view state you will see something like:

    <input type="hidden" name="__VIEWSTATE"
    value="dDwxNDg5OTk5MzM7Oz4itWBDehNJ4zs9hlG5LpoPAGTocw==" /

    Let's do a sample with a CheckBoxList and add the items with some code.

    .. Add a CheckBoxList to your page.
    .. Add the following code into the Page PreRender event to add four items
    the first time the page is loaded:

    <code (VB.NET) >
    If Me.IsPostBack = False Then

    With Me.CheckBoxList1

    .Items.Add("Item 1")
    .Items.Add("Item 2")
    .Items.Add("Item 3")
    .Items.Add("Item 4")

    End With

    End If
    </code>

    .. Run the project and click on the button a number of times. Notice the
    checkbox items remain on the page. Check the Source and observe the
    ViewState is significantly larger:

    <input type="hidden" name="__VIEWSTATE"
    value="dDwtMjAwNjc4MjQzNjt0PDtsPGk8MT47PjtsPHQ8O2w8aTw1Pjs+O2w8dDx0PDtwPGw8a
    TwwPjtpPDE+O2k8Mj47aTwzPjs+O2w8cDxJdGVtIDE7SXRlbSAxPjtwPEl0ZW0gMjtJdGVtIDI+O
    3A8SXRlbSAzO0l0ZW0gMz47cDxJdGVtIDQ7SXRlbSA0Pjs+Pjs+Ozs+Oz4+Oz4+O2w8Q2hlY2tCb
    3hMaXN0MTowO0NoZWNrQm94TGlzdDE6MTtDaGVja0JveExpc3QxOjI7Q2hlY2tCb3hMaXN0MTozO
    0NoZWNrQm94TGlzdDE6Mzs+Puu8f1UYuN6JR7A8eahu7/MHkwOv" /


    .. Now set the ViewState for the CheckBoxList control to False and run the
    project.
    .. Notice the first time the page displays the items display.
    .. Click the button and observe that the checkboxlist items no longer
    display. This is because the viewstate of the control was turned off and
    the list items no longer exist.

    -Sam Matzen




    -Sam Matzen


    "Philip Tripp" <> wrote in message
    news:eXll1%...
    > I've read numerous sources stating that view state can be disabled per
    > control, and per page, but can't seem to keep web form controls from
    > remembering their state on a postback. I'm using VS.Net 2002, .Net
    > Framework 1.0 SP2.
    >
    > I create a new project, and add a new web form to the project. On the
    > page properties, I set EnableViewState to False and verify that the @
    > Page directive has "enableViewState=false" in it. I then add a TextBox
    > control from the Web Froms, and set its EnableViewState property to
    > false. I add a Button control from the Web Forms, and start my project
    > (F5). I type in some text to the textbox, click the button so the form
    > submits, and expect that the text box come back empty (because view
    > state has been disabled), but it always comes back populated.
    >
    > I am new to ASP.NET, so am I somehow misunderstanding the dozen or so
    > aricles I've read about view state? Is view state always remembered on
    > post backs? If it is always remembered, then what the heck does setting
    > EnableViewState to false do?
    >
     
    Samuel L Matzen, Jul 25, 2004
    #3
  4. Philip Tripp

    Philip Tripp Guest

    Thank you for your information response, it clarifies things quite a bit.

    Samuel L Matzen wrote:
    > Philip,
    >
    > What you are seeing is the expected behavior.
    >
    > The ViewState will retain the state of your control if you populate it in
    > CodeBehind.
    >
    > If you enter data into the control in the browser you sill see this entered
    > value in the control in the code behind.
    >
    > ViewState will not change the behavior of a text box because after you enter
    > text into the textbox control the data is submitted to the server when you
    > click the button. Then the server takes the submitted value and places it
    > into the text property of the control. When the new page is rendered to the
    > broeser the text value is already set and will show up.
    >
    > The place that view state will make a lot of difference is with a treeview
    > where the data is never submitted back to the server.
    >
    > You can check to see if you have any view state by looking at the Source in
    > the browser. If there is view state you will see something like:
    >
    > <input type="hidden" name="__VIEWSTATE"
    > value="dDwxNDg5OTk5MzM7Oz4itWBDehNJ4zs9hlG5LpoPAGTocw==" /
    >
    > Let's do a sample with a CheckBoxList and add the items with some code.
    >
    > . Add a CheckBoxList to your page.
    > . Add the following code into the Page PreRender event to add four items
    > the first time the page is loaded:
    >
    > <code (VB.NET) >
    > If Me.IsPostBack = False Then
    >
    > With Me.CheckBoxList1
    >
    > .Items.Add("Item 1")
    > .Items.Add("Item 2")
    > .Items.Add("Item 3")
    > .Items.Add("Item 4")
    >
    > End With
    >
    > End If
    > </code>
    >
    > . Run the project and click on the button a number of times. Notice the
    > checkbox items remain on the page. Check the Source and observe the
    > ViewState is significantly larger:
    >
    > <input type="hidden" name="__VIEWSTATE"
    > value="dDwtMjAwNjc4MjQzNjt0PDtsPGk8MT47PjtsPHQ8O2w8aTw1Pjs+O2w8dDx0PDtwPGw8a
    > TwwPjtpPDE+O2k8Mj47aTwzPjs+O2w8cDxJdGVtIDE7SXRlbSAxPjtwPEl0ZW0gMjtJdGVtIDI+O
    > 3A8SXRlbSAzO0l0ZW0gMz47cDxJdGVtIDQ7SXRlbSA0Pjs+Pjs+Ozs+Oz4+Oz4+O2w8Q2hlY2tCb
    > 3hMaXN0MTowO0NoZWNrQm94TGlzdDE6MTtDaGVja0JveExpc3QxOjI7Q2hlY2tCb3hMaXN0MTozO
    > 0NoZWNrQm94TGlzdDE6Mzs+Puu8f1UYuN6JR7A8eahu7/MHkwOv" /
    >
    >
    > . Now set the ViewState for the CheckBoxList control to False and run the
    > project.
    > . Notice the first time the page displays the items display.
    > . Click the button and observe that the checkboxlist items no longer
    > display. This is because the viewstate of the control was turned off and
    > the list items no longer exist.
    >
    > -Sam Matzen
    >
    >
    >
    >
    > -Sam Matzen
    >
    >
    > "Philip Tripp" <> wrote in message
    > news:eXll1%...
    >
    >>I've read numerous sources stating that view state can be disabled per
    >>control, and per page, but can't seem to keep web form controls from
    >>remembering their state on a postback. I'm using VS.Net 2002, .Net
    >>Framework 1.0 SP2.
    >>
    >>I create a new project, and add a new web form to the project. On the
    >>page properties, I set EnableViewState to False and verify that the @
    >>Page directive has "enableViewState=false" in it. I then add a TextBox
    >>control from the Web Froms, and set its EnableViewState property to
    >>false. I add a Button control from the Web Forms, and start my project
    >>(F5). I type in some text to the textbox, click the button so the form
    >>submits, and expect that the text box come back empty (because view
    >>state has been disabled), but it always comes back populated.
    >>
    >>I am new to ASP.NET, so am I somehow misunderstanding the dozen or so
    >>aricles I've read about view state? Is view state always remembered on
    >>post backs? If it is always remembered, then what the heck does setting
    >>EnableViewState to false do?
    >>

    >
    >
    >
     
    Philip Tripp, Jul 26, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mark Hobley

    vec - a misunderstanding

    Mark Hobley, Mar 18, 2006, in forum: Perl
    Replies:
    1
    Views:
    2,613
    John Bokma
    Mar 18, 2006
  2. Kevin Spencer

    Re: Class property misunderstanding

    Kevin Spencer, Jun 26, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    836
    Kevin Spencer
    Jun 26, 2003
  3. Shawn Wildermuth

    Misunderstanding ConnectionStrings?

    Shawn Wildermuth, Jan 9, 2006, in forum: ASP .Net
    Replies:
    6
    Views:
    1,921
    Steven Cheng[MSFT]
    Jan 11, 2006
  4. Replies:
    3
    Views:
    578
    Flinky Wisty Pomm
    Apr 3, 2006
  5. Parthiv Joshi
    Replies:
    1
    Views:
    709
    Samuel L Matzen
    Jul 6, 2004
Loading...

Share This Page