View State Misunderstanding

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Philip Tripp

    View State Misunderstanding

    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 "enableViewStat e=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 misunderstandin g 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

    #2
    Re: View State Misunderstandin g

    what do you mean by "it always comes back populated.
    "
    "Philip Tripp" <tripppr@yahoo. com> wrote in message
    news:eXll1%23bc EHA.2840@TK2MSF TNGP11.phx.gbl. ..[color=blue]
    > 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 "enableViewStat e=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 misunderstandin g 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?
    >[/color]



    Comment

    • Samuel L Matzen

      #3
      Re: View State Misunderstandin g

      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="__VIEWSTA TE"
      value="dDwxNDg5 OTk5MzM7Oz4itWB DehNJ4zs9hlG5Lp oPAGTocw==" /

      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.CheckBoxList 1

      .Items.Add("Ite m 1")
      .Items.Add("Ite m 2")
      .Items.Add("Ite m 3")
      .Items.Add("Ite m 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="__VIEWSTA TE"
      value="dDwtMjAw Njc4MjQzNjt0PDt sPGk8MT47PjtsPH Q8O2w8aTw1Pjs+O 2w8dDx0PDtwPGw8 a
      TwwPjtpPDE+O2k8 Mj47aTwzPjs+O2w 8cDxJdGVtIDE7SX RlbSAxPjtwPEl0Z W0gMjtJdGVtIDI+ O
      3A8SXRlbSAzO0l0 ZW0gMz47cDxJdGV tIDQ7SXRlbSA0Pj s+Pjs+Ozs+Oz4+O z4+O2w8Q2hlY2tC b
      3hMaXN0MTowO0No ZWNrQm94TGlzdDE 6MTtDaGVja0JveE xpc3QxOjI7Q2hlY 2tCb3hMaXN0MToz O
      0NoZWNrQm94TGlz dDE6Mzs+Puu8f1U YuN6JR7A8eahu7/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" <tripppr@yahoo. com> wrote in message
      news:eXll1%23bc EHA.2840@TK2MSF TNGP11.phx.gbl. ..[color=blue]
      > 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 "enableViewStat e=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 misunderstandin g 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?
      >[/color]


      Comment

      • Philip Tripp

        #4
        Re: View State Misunderstandin g

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

        Samuel L Matzen wrote:[color=blue]
        > 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="__VIEWSTA TE"
        > value="dDwxNDg5 OTk5MzM7Oz4itWB DehNJ4zs9hlG5Lp oPAGTocw==" /
        >
        > 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.CheckBoxList 1
        >
        > .Items.Add("Ite m 1")
        > .Items.Add("Ite m 2")
        > .Items.Add("Ite m 3")
        > .Items.Add("Ite m 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="__VIEWSTA TE"
        > value="dDwtMjAw Njc4MjQzNjt0PDt sPGk8MT47PjtsPH Q8O2w8aTw1Pjs+O 2w8dDx0PDtwPGw8 a
        > TwwPjtpPDE+O2k8 Mj47aTwzPjs+O2w 8cDxJdGVtIDE7SX RlbSAxPjtwPEl0Z W0gMjtJdGVtIDI+ O
        > 3A8SXRlbSAzO0l0 ZW0gMz47cDxJdGV tIDQ7SXRlbSA0Pj s+Pjs+Ozs+Oz4+O z4+O2w8Q2hlY2tC b
        > 3hMaXN0MTowO0No ZWNrQm94TGlzdDE 6MTtDaGVja0JveE xpc3QxOjI7Q2hlY 2tCb3hMaXN0MToz O
        > 0NoZWNrQm94TGlz dDE6Mzs+Puu8f1U YuN6JR7A8eahu7/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" <tripppr@yahoo. com> wrote in message
        > news:eXll1%23bc EHA.2840@TK2MSF TNGP11.phx.gbl. ..
        >[color=green]
        >>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 "enableViewStat e=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 misunderstandin g 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
        >>EnableViewSta te to false do?
        >>[/color]
        >
        >
        >[/color]

        Comment

        Working...