473,473 Members | 1,754 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

View state

Hi,

I recently read an article on MSDN about viewstate and postback.
http://msdn.microsoft.com/library/de.../viewstate.asp

The author states the following:
"It is a common misconception among developers that view state is somehow
responsible for having TextBoxes, CheckBoxes, DropDownLists, and other Web
controls remember their values across postback. This is not the case, as the
values are identified via posted back form field values, and assigned in the
LoadPostData() method for those controls that implement
IPostBackDataHandler."

I would like to know exactly what ASP.NET controls don't need viewstate to
remeber value across postbacks.

thanx, Neven
Nov 19 '05 #1
3 2361
Any control who's value can be deduced from Request.Form.

However, note that even in the most obvious case, such as a textbox, the
viewstate still preserves the OLD value, which is the ONLY way for
TextChanged event to fire server-side.

Also, only the _selected_ value is preserved without viewstate. This means
if you have a radiobutton list with out viewstate, not only will your
Changed event not fire, but you won't have access to the non-selected radio
buttons.

In other words, all form elements can make due with only REquest.Form...if
you've programmed in classic ASP/PHP, the meaning of this should be
intuitive.

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/

"Neven Klofutar" <neven.klofutar@**re...m.o..v...e**vip.hr> wrote in message
news:em**************@TK2MSFTNGP10.phx.gbl...
Hi,

I recently read an article on MSDN about viewstate and postback.
http://msdn.microsoft.com/library/de.../viewstate.asp

The author states the following:
"It is a common misconception among developers that view state is somehow
responsible for having TextBoxes, CheckBoxes, DropDownLists, and other Web
controls remember their values across postback. This is not the case, as
the values are identified via posted back form field values, and assigned
in the LoadPostData() method for those controls that implement
IPostBackDataHandler."

I would like to know exactly what ASP.NET controls don't need viewstate to
remeber value across postbacks.

thanx, Neven

Nov 19 '05 #2
I see what you mean Karl, thanx !

Neven
"Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
wrote in message news:et**************@TK2MSFTNGP12.phx.gbl...
Any control who's value can be deduced from Request.Form.

However, note that even in the most obvious case, such as a textbox, the
viewstate still preserves the OLD value, which is the ONLY way for
TextChanged event to fire server-side.

Also, only the _selected_ value is preserved without viewstate. This
means if you have a radiobutton list with out viewstate, not only will
your Changed event not fire, but you won't have access to the non-selected
radio buttons.

In other words, all form elements can make due with only REquest.Form...if
you've programmed in classic ASP/PHP, the meaning of this should be
intuitive.

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/

"Neven Klofutar" <neven.klofutar@**re...m.o..v...e**vip.hr> wrote in
message news:em**************@TK2MSFTNGP10.phx.gbl...
Hi,

I recently read an article on MSDN about viewstate and postback.
http://msdn.microsoft.com/library/de.../viewstate.asp

The author states the following:
"It is a common misconception among developers that view state is somehow
responsible for having TextBoxes, CheckBoxes, DropDownLists, and other
Web controls remember their values across postback. This is not the case,
as the values are identified via posted back form field values, and
assigned in the LoadPostData() method for those controls that implement
IPostBackDataHandler."

I would like to know exactly what ASP.NET controls don't need viewstate
to remeber value across postbacks.

thanx, Neven


Nov 20 '05 #3
In an email follow up, Neven asked me if I could be more clear since he was
still seeing the TextChanged event fire despite turning off viewstate. Here
was my response, in case it helps others:

The TextChanged event is broken under special conditions, I wasn't

very clear about this.

Create a simple page:

<form id="form" method="post" runat="server">

<asp:Textbox ID="name" Runat="server" EnableViewState="False" />

<asp:Button ID="x" Runat="server" />

</form>

with the following codebehind:

protected TextBox name;

protected Button x;

private void Page_Load(object sender, EventArgs e)

{

name.TextChanged += new EventHandler(this.name_TextChanged);

x.Click += new EventHandler(this.x_Click);

}

private void name_TextChanged(object sender, EventArgs e)

{

Trace.Write("TextChanged");

}

private void x_Click(object sender, EventArgs e)

{

Trace.Write("ButtonClicked");

}

Enter text and click on the button, "TextChanged" WILL fire. However, now

CLEAR the text from the textbox and hit the button, "TextChange" WILL NOT

fire. Because the old value isn't preserved in viewstate, ASP.Net assumes

that the "old" value is blank. That means that textChanged won't fire if

you go from a value to a blank one (even though the value has changed).

Also, if you enter text, say "aa" and hit the button, the hit the button

again, you'll see that TextChanged fires. That's because it's comparing the

2nd "aa" to blank, instead of the old value which is actually also "aa".

Hope that makes some sense :)

The problem with the radio button list is for code like this:

private void Page_Load(object sender, EventArgs e)

{

if (!Page.IsPostback)

{

rad.DataSource = "XX";

rad.DataBind();

}

x.Click += new EventHandler(this.x_Click);

}

private void x_Click(object sender, EventArgs e)

{

Trace.Write(rad.Items[0].Value); //won't work unless 0 was selected

if viewstate is off for radiobutton list

}

The problem is that values are only added when it isn't postback

(!Page.IsPostBack). With viewstate on, no problem because the control is

re-created from viewstate, with it off however, it isn't recreated, and only

the selected value can be retrieved via Request.Form("rad"). One solution

is to re-bind the control, on postback as well (remove the check for

!PAge.IsPostback). you have to decide what you prefer, fetching the data

source again (might involve a database hit) or loading the data into

viewstate? I often prefer to reload the data, especially if it's cached!

Karl
--
MY ASP.Net tutorials
http://www.openmymind.net/
http://openmymind.net/redirector.aspx?documentId=51 - Learn about AJAX!

"Neven Klofutar" <ne***********************@vip.hr> wrote in message
news:eC**************@tk2msftngp13.phx.gbl...
I see what you mean Karl, thanx !

Neven
"Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
wrote in message news:et**************@TK2MSFTNGP12.phx.gbl...
Any control who's value can be deduced from Request.Form.

However, note that even in the most obvious case, such as a textbox, the
viewstate still preserves the OLD value, which is the ONLY way for
TextChanged event to fire server-side.

Also, only the _selected_ value is preserved without viewstate. This
means if you have a radiobutton list with out viewstate, not only will
your Changed event not fire, but you won't have access to the
non-selected radio buttons.

In other words, all form elements can make due with only
REquest.Form...if you've programmed in classic ASP/PHP, the meaning of
this should be intuitive.

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/

"Neven Klofutar" <neven.klofutar@**re...m.o..v...e**vip.hr> wrote in
message news:em**************@TK2MSFTNGP10.phx.gbl...
Hi,

I recently read an article on MSDN about viewstate and postback.
http://msdn.microsoft.com/library/de.../viewstate.asp

The author states the following:
"It is a common misconception among developers that view state is
somehow responsible for having TextBoxes, CheckBoxes, DropDownLists, and
other Web controls remember their values across postback. This is not
the case, as the values are identified via posted back form field
values, and assigned in the LoadPostData() method for those controls
that implement IPostBackDataHandler."

I would like to know exactly what ASP.NET controls don't need viewstate
to remeber value across postbacks.

thanx, Neven



Nov 20 '05 #4

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

Similar topics

3
by: Bren | last post by:
I am developing a web based app. in .net, with C# as the code behind.the problem I am having is with view state for a page. the page is capturing an event fired in an object. the event changes the...
0
by: Bren | last post by:
I am developing a web based app. in .net, with C# as the code behind.the problem I am having is with view state for a page. the page is capturing an event fired in an object. the event changes the...
2
by: Brad | last post by:
I have an intranet app that has just started sporadically getting the following error "The viewstate is invalid for this page and might be corrupted." By sproadic I mean 3-4 times during the past...
3
by: Philip Tripp | last post by:
I've read numerous sources stating that view state can be disabled per control, and per page, but can't seem to keep web form controls from remembering their state on a postback. I'm using VS.Net...
10
by: Zack Sessions | last post by:
Has anyone tried to create a SQL7 view using the CREATE VIEW command and ADO.NET? If so, is there a trick in trapping a SQL error when trying to create the view? I have a VB.NET app that, amoung...
20
by: raptor | last post by:
hi, I want to make the following thing : select-based updatable VIEW, which have two more virtual-fields. One of them is concatenation of others and the second is calculated on the fly. Can I...
3
by: gaDev | last post by:
1) I build a Html Table dynamically (Header Row, and then 2 rows with data All 2 rows have 2 cells: cell(0) contains a delete button (ASP Button), cell(1) contains a HTML Text box 2) On Form Load...
2
by: Michael | last post by:
Hi, I need to access view state data in my CreateChildControls method. Is there anyway to achieve that? I'm using ASP.NET 1.1. Thanks.
12
by: Doug | last post by:
Hi, I learned a little about the model view presenter pattern at a conference this last week and am experimenting with it. It's working pretty well but I have a question. I am trying to use...
9
by: Siddu | last post by:
Hi All, I am new to this group and this is my first doubt i am facing at present. I am doing data migration. In this sequence i need to alter few views. Alter in the sense, inside the...
0
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,...
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...
1
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...
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...
0
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,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
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...
0
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 ...
1
muto222
php
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.