473,769 Members | 6,739 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

problems understanding ViewState

Hi,

I have a Listbox with 3 strings added at design time : "Red" "Green"
"Blue".
I add a fourth string at run-time in Page_Load:

protected void Page_Load(objec t sender, EventArgs e)
{
if (!IsPostBack)
lstColors.Items .Add("Yellow");
}

Enabling the EnableViewState-property of the Listbox always shows the 4
strings.
Disabling the EnableViewState-property never shows "Yellow".

I find it very confusing: all 4 string are part of Listbox's ViewState
no ?
I'd say, or you see all 4 of them (when EnableViewState =true) or you
don't see anything listed (when EnableViewState =false).

But it doesn't work that way apparently. What is the logic behind
EnableViewState ?

It is confusing as well when using a Label: with EnableViewState set or
not, the Text property is ALWAYS shown, even when that text was set at
runtime.

So, what does ViewState really mean for a Control (ListBox, Label, ...)
?

Thank you
Chris


*** Sent via Developersdex http://www.developersdex.com ***
Aug 13 '07 #1
3 1411
Hi Chris,

if you turn off view state for your list box you should see the yellow item
only in first request to your page because you fill it in only when page is
not posted back. View state is actually a hidden field where all registered
controls store their state from previous request (encoded and usually
encrypted). This state is used when next request is precessed so if you turn
on view state all four items are stored to view state. When next request
arrives (post back) asp.net engine knows that list box contains four items
but if you turn off the view state asp.net engine does not have this
information and it has to fill list box again. But at this time only three
items with declarative definition are filled because yellow item is added
only if page was not posted back.

View state is usually turned off for controls binded to large dataset to
reduce size of http response. But there you have to rebind them in each time.

I hope this explanation helps you to understand the view state.

Regards,
Ladislav

"Chris Peeters" wrote:
Hi,

I have a Listbox with 3 strings added at design time : "Red" "Green"
"Blue".
I add a fourth string at run-time in Page_Load:

protected void Page_Load(objec t sender, EventArgs e)
{
if (!IsPostBack)
lstColors.Items .Add("Yellow");
}

Enabling the EnableViewState-property of the Listbox always shows the 4
strings.
Disabling the EnableViewState-property never shows "Yellow".

I find it very confusing: all 4 string are part of Listbox's ViewState
no ?
I'd say, or you see all 4 of them (when EnableViewState =true) or you
don't see anything listed (when EnableViewState =false).

But it doesn't work that way apparently. What is the logic behind
EnableViewState ?

It is confusing as well when using a Label: with EnableViewState set or
not, the Text property is ALWAYS shown, even when that text was set at
runtime.

So, what does ViewState really mean for a Control (ListBox, Label, ...)
?

Thank you
Chris


*** Sent via Developersdex http://www.developersdex.com ***
Aug 13 '07 #2
Hi Ladislav,

thank you for your explanation !

I understand the Postback mechanism but what is still unclear to me is
why are the 3 other strings (red, green, blue) shown even when
EnableViewState is turned off ?

thank you
Chris

*** Sent via Developersdex http://www.developersdex.com ***
Aug 13 '07 #3
Hi Chris,

Label is processed in same way as any other web control. If you set Text
attribute in asp:Label tag (means you set the Text in design time) it will
always be used as initialization. ViewState stores only values assigned at
runtime. Also if you use something like this:
protected void Page_Load(objec t sender, EventArgs e)
{
MyLabel.Text = "some text";
}
you will override the value (at runtime) in each request = ViewState can be
turned off and behavior will be the same as when you turn the ViewState on
and use:
protected void Page_Load(objec t sender, EventArgs e)
{
if (!IsPostBack)
{
MyLabel.Text = "some text";
}
}

Regards,
Ladislav

"Chris Peeters" wrote:
Hello Ladislav,

it's clearer now for the Listbox !

Maybe a final question: what about when using a Label: with
EnableViewState set or not for the label-control, the Text property is
ALWAYS shown, even when that text was set at runtime ???

thank you
Chris

*** Sent via Developersdex http://www.developersdex.com ***
Aug 13 '07 #4

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

Similar topics

8
3786
by: Cathie | last post by:
Hi guys, I want to do a Server.Transfer to get to a second page, so that I may retrieve variables I have set in the first page. I'm doing that with the usual Server.Transfer("pagename", true). My problems are as follows: 1. On the initial load everything seems to be referencing from the first page. For example, my style sheet I reference using "../../styles.css" but it seems to need "../styles.css" as the first page is one step...
1
2418
by: Steph | last post by:
I am trying to save a simple integer value in ViewState. I set the value in the Page_Load() event and retrieve it in the function Page_Unload() event. The value seems to be set correctly, but upon the next Page_Load(), I get the following error when I try to retrieve the value from ViewState: "function 'ViewState.get_Item' evaluated and returned null". The page does have a form on it with the runat="server" attribute set. From the...
2
1357
by: BluDog | last post by:
Hi I have a created a custom web control called ImageBrowser, extract is below: <Code> #Region "Properties" Public Property Images() As ImageCollection
1
1933
by: S S Venkatesh | last post by:
Hi, I'm involved in a ASP.NET web project with a web farm and load balancing(windows server 2003 ) with no afinity (no sticky sessions) and i am facing problems in view state. I get an error like this. Server Error in '/ViewState' Application. ---------------------------------------------------------------------------- ---- The viewstate is invalid for this page and might be corrupted.
4
9002
by: Harry | last post by:
Hello, I have a Repeater control that contains a Label control inside its ItemTemplate. A dummy mockup of the HTML code looks as follows: <asp:repeater id="StyleRepeater" Runat="server"> <ItemTemplate> <asp:Label ID="ClassLabel" Runat="server"> <... various other ASP.NET Web Controls ...> </ItemTemplate> </asp:repeater>
12
1864
by: TB | last post by:
Hi All: I am trying to create a variation on the standard datagrid, whereby the datagrid is only shown after pressing some buttons. This reason for this is that I would like to use the same datagrid for several tables, and the idea is that the button events store the the SQL select statement and the SQL update statement in view state items, which can the be reused for all the datagrid events (paging and editing). However I seem to...
0
778
by: Jawahar Rajan | last post by:
All, Have any of you experienced any problems using software like SecureIIS on IIS 5.0 and a ASP.net application. We seem to be having some problems with the Viewstate. The SecureIIS monitors all POST query and for some reason it does not like the length of the viewstate. Even if we increase the allowed length to 100, 000 there are still problems as we can never be sure of the length f the view state?
0
1889
by: David | last post by:
Hello all. I am trying to implement my first server control and have run into two problems that I cannot solve. I need the assistance of someone with more experience. My goal was to create an input button that would allow for both text and an image on it. I am attempting to accomplish this by basing off of the asp:button control. I added a property for the image url and one to determine if the image is displayed on the left or right...
3
1860
by: moshi | last post by:
Hey, I'm working with asp.net 2.0. I make objects (like ddl, textbox...) disabled=true in client side. Then, when I get to the server in post action, these objects lose their value. For example: ddl comes back after postback with the first values in it's list - it doesn't keep the new value that the user chooses, and textbox - still remains it's last value, like the user didn't alter it. What I'm trying to say is that making object on...
0
9589
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
10216
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
10049
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...
1
9997
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
8873
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
6675
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
5310
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...
1
3965
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
2
3565
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.