473,387 Members | 3,684 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,387 software developers and data experts.

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(object 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 1394
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(object 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(object 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(object 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
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). ...
1
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...
2
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
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...
4
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">...
12
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...
0
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...
0
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...
3
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:...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
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
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
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...

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.