473,398 Members | 2,212 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,398 software developers and data experts.

How do I know when to use the Viewstate and when to use the posted data? :-)

Hi everyone,

I have a quick question that I hope someone can help me with:

I've made a user control that contains a text box and some validation
functionality. This control has a few extra properties declared on it over
and above that provided by the textbox.

As I understand it, part of the process to make this work is, in the
property accessor's for the properties that you wish to persist you add
values to the ViewState and you remove values to the viewstate. I don't
quite understand why yet but I think its something to do with maintaining
state over postbacks.

OK so far.

When my control is loaded, it goes to the database and gets the information
it needs to display. I'm not sure if I'm doing the right thing here or not,
but I'll explicitly state my question in a couple of second.

Once it gets the data it needs, it uses the set property accessors to
initialise the control and have the values dropped into viewstate.

When the page load in front of the user it seems to me that the page will
have the values of internal controls actually in the controls themselves (as
you'd expect), but also in the viewstate. Fair enough.

My actual question is related to the following:

When the user hits a submit button on the page amongst the other things that
occur, the following happens:

1. The values of the controls are posted back to me. If the user has changed
any values in the textboxes or so on, thats where the changed will be (I
think).

2. Somehow, the viewstate is made available. I'm guessing that it's posted
back as well, which seems a bit of duplicated effort but never mind but I
guess the viewstate and the post variables may contain different info on
occasions that the user has changed a value on the page. I dont have time to
worry about that just now unfortunately.

Question:

Given that I have the viewstate and then also the actual values posted
straight from the controls themselves the question is, do I just ignore the
viewstate because it doesnt contain the most recent values or do I have to
pay attention to it somehow. If the former is the case, then I don't see the
point in the viewstate. If the later is the case, then I still don't see the
point of the viewstate because as I understand it, all its going to contain
is the values that were sent to the page on the way out. The value in the
viewstate won't have changed when they come back in on a postback. The
values of the controls may have been changed by the user, but then thats
seperate from the viewstate (I think).

If anyone can help me understand this without using the phrase "Viewstate is
used to persist form values over postbacks" or something to that effect,
then I would be very greatful.

With many thanks in advance

Simon
Nov 18 '05 #1
1 1749
Hi Simon,

As far as I know, Viewstate is used to persist form values over postbacks
only when you want to know if value has changed.

For instance, try this :
<input runat="server" type="text" name="myHtmlInputText" />
You don't specify that you want to know if value has changed between posts,
so viewstate isn't used (try it with trace="true") but the value is still
persisted.

Now if you write :
<input runat="server" type="text" name="myHtmlInputText"
onServerChange="myHtmlInputText_ServerChange" />
You specify that you want to call myHtmlInputText_ServerChange when value
has changed, so ViewState is used here: old value is saved in viewstate and
then compared to the new one from Input to detect a change.

Hope it helps
Sorry for my bad English
Henri

"Simon" <sh856531@microsofts_free_email_service.com> a écrit dans le message
de news:eI**************@TK2MSFTNGP11.phx.gbl...
Hi everyone,

I have a quick question that I hope someone can help me with:

I've made a user control that contains a text box and some validation
functionality. This control has a few extra properties declared on it over
and above that provided by the textbox.

As I understand it, part of the process to make this work is, in the
property accessor's for the properties that you wish to persist you add
values to the ViewState and you remove values to the viewstate. I don't
quite understand why yet but I think its something to do with maintaining
state over postbacks.

OK so far.

When my control is loaded, it goes to the database and gets the information it needs to display. I'm not sure if I'm doing the right thing here or not, but I'll explicitly state my question in a couple of second.

Once it gets the data it needs, it uses the set property accessors to
initialise the control and have the values dropped into viewstate.

When the page load in front of the user it seems to me that the page will
have the values of internal controls actually in the controls themselves (as you'd expect), but also in the viewstate. Fair enough.

My actual question is related to the following:

When the user hits a submit button on the page amongst the other things that occur, the following happens:

1. The values of the controls are posted back to me. If the user has changed any values in the textboxes or so on, thats where the changed will be (I
think).

2. Somehow, the viewstate is made available. I'm guessing that it's posted
back as well, which seems a bit of duplicated effort but never mind but I
guess the viewstate and the post variables may contain different info on
occasions that the user has changed a value on the page. I dont have time to worry about that just now unfortunately.

Question:

Given that I have the viewstate and then also the actual values posted
straight from the controls themselves the question is, do I just ignore the viewstate because it doesnt contain the most recent values or do I have to
pay attention to it somehow. If the former is the case, then I don't see the point in the viewstate. If the later is the case, then I still don't see the point of the viewstate because as I understand it, all its going to contain is the values that were sent to the page on the way out. The value in the
viewstate won't have changed when they come back in on a postback. The
values of the controls may have been changed by the user, but then thats
seperate from the viewstate (I think).

If anyone can help me understand this without using the phrase "Viewstate is used to persist form values over postbacks" or something to that effect,
then I would be very greatful.

With many thanks in advance

Simon


Nov 18 '05 #2

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

Similar topics

6
by: vinay | last post by:
What is this error?? Exception Details: System.Web.HttpException: Unable to validate data. System.Web.Configuration.MachineKey.GetDecodedData(Byte buf, Byte modifier, Int32 start, Int32...
2
by: Alan Silver | last post by:
Hello, I'm having rather a problem with user control. It is a fairly simple affair (see my other threads for more details) that shows a date and time in five drop down controls. I had private...
5
by: No one | last post by:
Here is the view state error I am getting on a Windows 2003 box: ------------------------------------------------- Unable to validate data. Description: An unhandled exception occurred during...
5
by: Richard Myers | last post by:
Hello I have a datagrid that has a TemplatedColumn containing a dropdown list (DDL). This DDL is filled for each datagrid row via the itemcreated created event. The first time i postback none...
0
by: rgouge | last post by:
I have a datagrid on a page .. which can be very large .. a couple of hundred rows and 20 columns. Some of the columns are custom templates allowing the user to enter in text or select a checkbox...
5
by: sjl | last post by:
I've got an .aspx webform for searching my database. It basically takes user input and passes it as a parm into a stored proc to search a table. The results are returned in a SQLDataReader and...
2
by: Carly | last post by:
Hi, I am now not sure I understand what ViewState does. Having EnableViewState=true or false on a WEB form and/or different server controls does not seem to make any difference. I am just...
11
by: =?Utf-8?B?R2lsZXMgUGFwd29ydGg=?= | last post by:
I seem to be getting this error message all of a sudden on one of my web apps: System.Web.Configuration.MachineKey.GetDecodedData(Byte buf, Byte modifier, Int32 start, Int32 length, Int32&...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...

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.