473,756 Members | 4,046 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1769
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="myHtmlInp utText" />
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="myHtmlInp utText"
onServerChange= "myHtmlInputTex t_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@micro softs_free_emai l_service.com> a écrit dans le message
de news:eI******** ******@TK2MSFTN GP11.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
6044
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 length, Int32& dataLength) +195 System.Web.UI.LosFormatter.Deserialize(String input) +60
2
2498
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 member variables for the day, month, year, hour and minute, and the public property that sets the DateTime simply stored the relevant numbers in these variables.
5
2317
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 the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.Web.HttpException: Unable to validate data.
5
1359
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 of the selections made by the user are available for each dropdownlist on the server. They all simply have values equal to the first record of the datasource.... which is not very useful.
0
913
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 .. since there are several postbacks while interacting with the page .. reducing the viewstate size is important. I've thought about setting the viewstate to false on the grid and rebinding on a postback but how would I go about capturing the...
5
3277
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 bound to a datagrid on the same page. It works 90% of the time as expected. However, for some unknown reason, I randomly get the error below. The HttpException says there is an 'Invalid_Viewstate'. Ok, but why? I'm posting back to the same page. If...
2
265
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 playing around with VS 2005 and no matter the value of EnableViewState all the controls maintain their value. Can anybody explain or point me to a clear article about this subject. Much appreciated,
11
8693
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& dataLength) +195 System.Web.UI.LosFormatter.Deserialize(String input) +60 System.Web.UI.LosFormatter.Deserialize(String input) +117 System.Web.UI.Page.LoadPageStateFromPersistenceMedium() +102
0
9431
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9255
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10014
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9844
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9689
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8688
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6514
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5119
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2647
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.