473,554 Members | 2,946 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ViewState questions

et
I'm not sure I understand the use of the ViewState.

Do I understand correctly that values of controls are automatically held in
a hidden control called ViewState?

If so, then why can't we get them, or how do we get that value? I always
have to set the ViewState("Firs tName") = txtBox.text myself before a
postback is done, otherwise, the value of ViewState("Firs tName") is always
empty; there doesn't seem to be a way to retrieve that information when
needed unless manually set.

If I am setting the value of the ViewState myself, then aren't I doing
duplicate work if the value of the control should already be held somewhere?
And doesn't that meant that this hidden control is taking up resources when
it's unretrieveable, so why use it.

Is it more likely that the ViewState is just like the Session, except it
lasts for only a page, and is lost when the page changes.

And if so, does that mean there is a viewstate sitting there taking up
resources that we can't get. Should I turn it off on each control, and then
manually set ViewState myself?

Thanks for all your help, I've read an awful lot of articles already but if
you find a good one, let me know.
Nov 19 '05 #1
7 2033
ViewState("Firs tName") = txtBox.text means you add your own data to
ViewState. You needn't do anything if you use txtBox.ViewStat e("Text").
Nov 19 '05 #2
Microsfot has some good articles on ViewState, Visual Studio magazine does
as well...but here's some simple answers to your questions...

"et" <ea************ *@yahoo.com> wrote in message
news:ul******** ********@TK2MSF TNGP09.phx.gbl. ..
I'm not sure I understand the use of the ViewState.

Do I understand correctly that values of controls are automatically held
in a hidden control called ViewState? A: Yes, as long as the EnableViewState propery is set to True

If so, then why can't we get them, or how do we get that value? I always
have to set the ViewState("Firs tName") = txtBox.text myself before a
postback is done, otherwise, the value of ViewState("Firs tName") is always
empty; there doesn't seem to be a way to retrieve that information when
needed unless manually set. A: You can, but "FirstName" clearly is not the name of the control
"txtBox". However, you shouldn't ever need to access values for a control
from ViewState. Make sure it's and ASP Web Control with "runat" set to
server. It needs to be a server control to work with viewstate.

If I am setting the value of the ViewState myself, then aren't I doing
duplicate work if the value of the control should already be held
somewhere? And doesn't that meant that this hidden control is taking up
resources when it's unretrieveable, so why use it. A: Well, the control is saving to ViewState if it's setup properly. To
answer your question, yes, if you have a hidden control to store a value and
you store the value yourself in ViewState, then you are duplicating efforts.
Because the webcontrol is already designed to work with ViewState and the
postback functionality, it's best practice to take the hidden control
approach. Less hand rolling of code.

Is it more likely that the ViewState is just like the Session, except it
lasts for only a page, and is lost when the page changes. A: Correct... very similar to a Session variable, but at the page
level. It keeps you from using memory on the server and solves the client
cookies problem. It's much more powerful than that, however. To duplicate
it's fucntionality in classic ASP, a devloper would have to write hundreds
of lines of code, manage cookies, session objects, and state. Now, ASP.NET
does it all for you.
And if so, does that mean there is a viewstate sitting there taking up
resources that we can't get. Should I turn it off on each control, and
then manually set ViewState myself? A: Yes and no. The ViewState is being used, and the only resources it
uses is band width...it makes the resulting html larger. If you don't need
ViewState for a particular control, then yes, you should disable it.
Thanks for all your help, I've read an awful lot of articles already but
if you find a good one, let me know.

Nov 19 '05 #3
ViewState is used to maintain state between a render and a postback, and
indead its stored in a hidden field. any control can store any needed state
information in viewstate. some controls will honor the enableviewstate
property. most control do not need to store their value in viewstate, as the
browser will post it. a control like the datagrid, which render html, and
gets no postback data, needs to store a lot of info in viewstate - the value
of every cell.

-- bruce (sqlwork.com)


"et" <ea************ *@yahoo.com> wrote in message
news:ul******** ********@TK2MSF TNGP09.phx.gbl. ..
| I'm not sure I understand the use of the ViewState.
|
| Do I understand correctly that values of controls are automatically held
in
| a hidden control called ViewState?
|
| If so, then why can't we get them, or how do we get that value? I always
| have to set the ViewState("Firs tName") = txtBox.text myself before a
| postback is done, otherwise, the value of ViewState("Firs tName") is always
| empty; there doesn't seem to be a way to retrieve that information when
| needed unless manually set.
|
| If I am setting the value of the ViewState myself, then aren't I doing
| duplicate work if the value of the control should already be held
somewhere?
| And doesn't that meant that this hidden control is taking up resources
when
| it's unretrieveable, so why use it.
|
| Is it more likely that the ViewState is just like the Session, except it
| lasts for only a page, and is lost when the page changes.
|
| And if so, does that mean there is a viewstate sitting there taking up
| resources that we can't get. Should I turn it off on each control, and
then
| manually set ViewState myself?
|
| Thanks for all your help, I've read an awful lot of articles already but
if
| you find a good one, let me know.
|
|
Nov 19 '05 #4
et
There is no txtBox.ViewStat e in the intellisense list... Any other ideas??
"Rulin Hong" <Ru*******@disc ussions.microso ft.com> wrote in message
news:B7******** *************** ***********@mic rosoft.com...
ViewState("Firs tName") = txtBox.text means you add your own data to
ViewState. You needn't do anything if you use txtBox.ViewStat e("Text").

Nov 19 '05 #5
et
This is some great information, thanks! But I still don't understand how
you get information from the view state??? Why have it if you can't get it?
If so, then why can't we get them, or how do we get that value? I always have to set the ViewState("Firs tName") = txtBox.text myself before a
postback is done, otherwise, the value of ViewState("Firs tName") is always empty; there doesn't seem to be a way to retrieve that information when needed unless manually set. A: You can, but "FirstName" clearly is not the name of the

control "txtBox". However, you shouldn't ever need to access values for a control from ViewState. Make sure it's and ASP Web Control with "runat" set to server. It needs to be a server control to work with viewstate.
Okay, why have a viewstate if you can't get the value of controls for
them??? During a postback, the text that a user enters in a textbox, or
even if it's hardcoded, is lost unless you retrieve it from somewhere. If I
do
dim x as String = ViewState(txtBo x.text)
on postback, x is nothing, but I know there had been a value in the textbox
before the postback, so I need to get the value of that textbox.


"Jason Penniman" <jp*******@actc ci.com> wrote in message
news:%2******** ********@TK2MSF TNGP11.phx.gbl. .. Microsfot has some good articles on ViewState, Visual Studio magazine does
as well...but here's some simple answers to your questions...

"et" <ea************ *@yahoo.com> wrote in message
news:ul******** ********@TK2MSF TNGP09.phx.gbl. ..
I'm not sure I understand the use of the ViewState.

Do I understand correctly that values of controls are automatically held
in a hidden control called ViewState?

A: Yes, as long as the EnableViewState propery is set to True

If so, then why can't we get them, or how do we get that value? I always
have to set the ViewState("Firs tName") = txtBox.text myself before a
postback is done, otherwise, the value of ViewState("Firs tName") is
always empty; there doesn't seem to be a way to retrieve that information
when needed unless manually set.

A: You can, but "FirstName" clearly is not the name of the control
"txtBox". However, you shouldn't ever need to access values for a control
from ViewState. Make sure it's and ASP Web Control with "runat" set to
server. It needs to be a server control to work with viewstate.

If I am setting the value of the ViewState myself, then aren't I doing
duplicate work if the value of the control should already be held
somewhere? And doesn't that meant that this hidden control is taking up
resources when it's unretrieveable, so why use it.

A: Well, the control is saving to ViewState if it's setup properly.
To answer your question, yes, if you have a hidden control to store a
value and you store the value yourself in ViewState, then you are
duplicating efforts. Because the webcontrol is already designed to work
with ViewState and the postback functionality, it's best practice to take
the hidden control approach. Less hand rolling of code.

Is it more likely that the ViewState is just like the Session, except it
lasts for only a page, and is lost when the page changes.

A: Correct... very similar to a Session variable, but at the page
level. It keeps you from using memory on the server and solves the client
cookies problem. It's much more powerful than that, however. To
duplicate it's fucntionality in classic ASP, a devloper would have to
write hundreds of lines of code, manage cookies, session objects, and
state. Now, ASP.NET does it all for you.

And if so, does that mean there is a viewstate sitting there taking up
resources that we can't get. Should I turn it off on each control, and
then manually set ViewState myself?

A: Yes and no. The ViewState is being used, and the only resources it
uses is band width...it makes the resulting html larger. If you don't
need ViewState for a particular control, then yes, you should disable it.

Thanks for all your help, I've read an awful lot of articles already but
if you find a good one, let me know.


Nov 19 '05 #6
You dont need to get the information from the viewstate. The control will
save its state to the viewstate and load it from the viewstate. A serverside
textbox should have the text property populated in a postback because it
does it itself.

Ciaran
"et" <ea************ *@yahoo.com> wrote in message
news:uT******** ******@TK2MSFTN GP09.phx.gbl...
There is no txtBox.ViewStat e in the intellisense list... Any other
ideas??
"Rulin Hong" <Ru*******@disc ussions.microso ft.com> wrote in message
news:B7******** *************** ***********@mic rosoft.com...
ViewState("Firs tName") = txtBox.text means you add your own data to
ViewState. You needn't do anything if you use txtBox.ViewStat e("Text").


Nov 19 '05 #7
et < ea************* @yahoo.com > wrote:
This is some great information, thanks! But I still don't understand how
you get information from the view state??? Why have it if you can't get
it?


The Viewstate data for a TextBox is hidden from the parent control because
quite simply, the parent doesn't need to know about it. That's the essence
of object oriented design - hide away the implementation details from
classes which don't need to know about them.

>> If so, then why can't we get them, or how do we get that
> value? I always >> have to set the ViewState("Firs tName") = txtBox.text myself
> before a postback is done, otherwise, the value of
> ViewState("Firs tName") is always >> empty; there doesn't seem to be a way to retrieve that
> information when >> needed unless manually set.
> A: You can, but "FirstName" clearly is not the name of the control > "txtBox". However, you shouldn't ever need to access values for a control > from ViewState. Make sure it's and ASP Web Control with "runat"
set to > server. It needs to be a server control to work with viewstate.

Okay, why have a viewstate if you can't get the value of controls for
them??? During a postback, the text that a user enters in a textbox, or
even if it's hardcoded, is lost unless you retrieve it from somewhere. If
I do
dim x as String = ViewState(txtBo x.text)
on postback, x is nothing, but I know there had been a value in the
textbox before the postback, so I need to get the value of that textbox.


A TextBox's Text property has nothing to do with Viewstate (unless you're
concerned with the TextChanged event, in which case the Viewstate will store
the *previous* value of Text). Whenever you have a TextBox on a form, fill
it out, and submit the form, the Text property will be populated
automatically via Postback, as long as:

1) The TextBox has been created and added to the Controls collection or a
control in the Controls collection, etc.
2) The TextBox has been added before Postback data is loaded (during Init is
best, Load might work somtimes, but it's technically late)
3) The TextBox is given the same ID each request

If your TextBox has ID "txtBox", and these three conditions are met, then
you can retrieve the Text property of a TextBox (in, say, the Load or
PreRender events) by:

string txt = txtBox.Text;

Does that make sense?
Nov 19 '05 #8

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

Similar topics

0
2328
by: Trevor Andrew | last post by:
Hi There, I have posted something previously regarding this issue, but I think I have some more concise questions to ask, and would like to get further feedback on this issue. Firstly the background. I have a small ASP.NET application under development. I'm using VS 2002 with Framework 1.1. One of the pages is a "resources" page that...
2
2808
by: Ben Rush | last post by:
Hello World, Okay, I have spent the day browsing the newsgroups and reading up on article after article concerning ViewState corruption and so forth, and I have a couple questions. We intermittantly get the following exception - EXCEPTION MESSAGE: The viewstate is invalid for this page and might be corrupted. STACK TRACE:
10
2247
by: neo | last post by:
hi, I am studying ASP.NET and have few questions - 1) The session ID and values of controls is stored in VIEWSTATE variable. So now when we put EnableViewState="false" in Page directive and disable the session state in Web.Config the VIEWSTATE variable is still maintained and stores some values. Can anyone tell what those values are for,...
8
4265
by: Invalidlastname | last post by:
Hi, We are developing an asp.net application, and we dynamically created certain literal controls to represent some read-only text for certain editable controls. However, recently we found an issue which is related to the repeater. In the code shown below, if I call Repeater1.Controls.Count in the OnInit (the code fragment was highlighted in...
1
1699
by: Sky | last post by:
Although I've been using C# for the last month or so, and accepting out of blind faith the ViewState, I do have some nagging questions about it... can you help verify the following statements? Application is across all sessions. Session is across all pages. ViewState is sort of like Session, but for only one page. <-- Correct statement? ...
3
1165
by: foldface | last post by:
Hi I have only just become aware that form values posted to the server don't need to be preserved in viewstate because asp.net preserves them anyway separately from this. 2 questions (1) What is the mechanism that preserves them? (Purely for academic interest) (2) I have a property on a control that is added to the viewstate. With trace on...
4
6720
by: Chuck Ritzke | last post by:
Hi, I've searched the newsgroup and other sources to understand how to handle runtime controls and see I'm not the only one who's confused, but I'm still not quite sure of the best way to handle from all the various explanations/answers. I'm attempting the typical scenario... I create a variable number of controls at runtime based on...
0
1447
by: S.Sigal | last post by:
Hello: I've been trying to 'organize' the layout of my larger controls by moving variables into instances of subclasses...but it just dawned on me that I might be opening a real can of worms due to StateBag serialization... If ViewState serialization is optimized for Int32, Bool, String, Color, and arrays thereof... what happens when I...
0
1656
by: Walter | last post by:
Hi, can someone please help me with my custom control viewstate problem....I haven't slept for hours trying to get this fixed. I am making two custom controls which will be included on a single page. For now, I will call them box1 and box2. Each box has a panel and will display a list of link buttons on the panel. When the page loads for...
0
7578
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...
0
7497
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...
0
7780
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. ...
1
7530
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6119
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...
0
3539
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...
0
3530
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1994
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1111
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.