473,395 Members | 1,497 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,395 software developers and data experts.

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("FirstName") = txtBox.text myself before a
postback is done, otherwise, the value of ViewState("FirstName") 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 2023
ViewState("FirstName") = txtBox.text means you add your own data to
ViewState. You needn't do anything if you use txtBox.ViewState("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****************@TK2MSFTNGP09.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("FirstName") = txtBox.text myself before a
postback is done, otherwise, the value of ViewState("FirstName") 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****************@TK2MSFTNGP09.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("FirstName") = txtBox.text myself before a
| postback is done, otherwise, the value of ViewState("FirstName") 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.ViewState in the intellisense list... Any other ideas??
"Rulin Hong" <Ru*******@discussions.microsoft.com> wrote in message
news:B7**********************************@microsof t.com...
ViewState("FirstName") = txtBox.text means you add your own data to
ViewState. You needn't do anything if you use txtBox.ViewState("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("FirstName") = txtBox.text myself before a
postback is done, otherwise, the value of ViewState("FirstName") 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(txtBox.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*******@actcci.com> wrote in message
news:%2****************@TK2MSFTNGP11.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****************@TK2MSFTNGP09.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("FirstName") = txtBox.text myself before a
postback is done, otherwise, the value of ViewState("FirstName") 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**************@TK2MSFTNGP09.phx.gbl...
There is no txtBox.ViewState in the intellisense list... Any other
ideas??
"Rulin Hong" <Ru*******@discussions.microsoft.com> wrote in message
news:B7**********************************@microsof t.com...
ViewState("FirstName") = txtBox.text means you add your own data to
ViewState. You needn't do anything if you use txtBox.ViewState("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("FirstName") = txtBox.text myself
> before a postback is done, otherwise, the value of
> ViewState("FirstName") 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(txtBox.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
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...
2
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...
10
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...
8
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...
1
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? ...
3
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...
4
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...
0
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...
0
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...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
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
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.