473,408 Members | 1,955 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

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 "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?

Nov 18 '05 #1
3 2098
what do you mean by "it always comes back populated.
"
"Philip Tripp" <tr*****@yahoo.com> wrote in message
news:eX****************@TK2MSFTNGP11.phx.gbl...
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?


Nov 18 '05 #2
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="dDwxNDg5OTk5MzM7Oz4itWBDehNJ4zs9hlG5LpoPAGT ocw==" /

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="dDwtMjAwNjc4MjQzNjt0PDtsPGk8MT47PjtsPHQ8O2w 8aTw1Pjs+O2w8dDx0PDtwPGw8a
TwwPjtpPDE+O2k8Mj47aTwzPjs+O2w8cDxJdGVtIDE7SXRlbSA xPjtwPEl0ZW0gMjtJdGVtIDI+O
3A8SXRlbSAzO0l0ZW0gMz47cDxJdGVtIDQ7SXRlbSA0Pjs+Pjs +Ozs+Oz4+Oz4+O2w8Q2hlY2tCb
3hMaXN0MTowO0NoZWNrQm94TGlzdDE6MTtDaGVja0JveExpc3Q xOjI7Q2hlY2tCb3hMaXN0MTozO
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" <tr*****@yahoo.com> wrote in message
news:eX****************@TK2MSFTNGP11.phx.gbl...
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?

Nov 18 '05 #3
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="dDwxNDg5OTk5MzM7Oz4itWBDehNJ4zs9hlG5LpoPAGT ocw==" /

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="dDwtMjAwNjc4MjQzNjt0PDtsPGk8MT47PjtsPHQ8O2w 8aTw1Pjs+O2w8dDx0PDtwPGw8a
TwwPjtpPDE+O2k8Mj47aTwzPjs+O2w8cDxJdGVtIDE7SXRlbSA xPjtwPEl0ZW0gMjtJdGVtIDI+O
3A8SXRlbSAzO0l0ZW0gMz47cDxJdGVtIDQ7SXRlbSA0Pjs+Pjs +Ozs+Oz4+Oz4+O2w8Q2hlY2tCb
3hMaXN0MTowO0NoZWNrQm94TGlzdDE6MTtDaGVja0JveExpc3Q xOjI7Q2hlY2tCb3hMaXN0MTozO
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" <tr*****@yahoo.com> wrote in message
news:eX****************@TK2MSFTNGP11.phx.gbl...
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?



Nov 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Bren | last post by:
I am developing a web based app. in .net, with C# as the code behind.the problem I am having is with view state for a page. the page is capturing an event fired in an object. the event changes the...
2
by: Ringo Langly | last post by:
Hi everyone, I have 5 servers, all with identical databases just different data. I have a rather lengthy SQL statement (in a View) to hit one database and pull-in certain data, but I'd like to...
0
by: Bren | last post by:
I am developing a web based app. in .net, with C# as the code behind.the problem I am having is with view state for a page. the page is capturing an event fired in an object. the event changes the...
2
by: Brad | last post by:
I have an intranet app that has just started sporadically getting the following error "The viewstate is invalid for this page and might be corrupted." By sproadic I mean 3-4 times during the past...
10
by: Zack Sessions | last post by:
Has anyone tried to create a SQL7 view using the CREATE VIEW command and ADO.NET? If so, is there a trick in trapping a SQL error when trying to create the view? I have a VB.NET app that, amoung...
20
by: raptor | last post by:
hi, I want to make the following thing : select-based updatable VIEW, which have two more virtual-fields. One of them is concatenation of others and the second is calculated on the fly. Can I...
12
by: Doug | last post by:
Hi, I learned a little about the model view presenter pattern at a conference this last week and am experimenting with it. It's working pretty well but I have a question. I am trying to use...
3
by: Nathan Sokalski | last post by:
I am recieving the following error on the second postback of a page I have written: The state information is invalid for this page and might be corrupted Stack Trace: ...
4
by: =?Utf-8?B?Z3V5?= | last post by:
Is this a valid approach to State Management? What I do is define a class 'State' which holds all the objects etc. that I need to store in the Session. I can then get my state object and...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.