I am having trouble turning off viewstate for the controls in a page/form. Whatever I try the controls preserve & display the user input. I first turned viewstate off at control level, then page level and finally site level,yet still the form repopulates
with values entered.
Inspite of all this the textbox, checkboxes, dropdowns etc preserve and display the user input on postback. Obviously I am doing something 'dumb' or laboring under a misconception. I'd like to know what!
Incidentally a gridview on the same page which displays all records from the table the form is used to fill can be disabled, ensuring it shows all records + the new one).
I might be wrong but you seem to be having completely wrong idea on viewstate.
Viewstate is used by web controls to preserve their states/properties. Like the cssclass property of your textbox, not it's values.
But when your page postbacks, what asp.net does is to fill the values in your controls from the form that has been posted. I hope you know what form is. form is the collection of key value pair values from your web page when posted back to the server. Asp.net
simply maps the control's name in the form collection and set's its value
Great article nilsan! It pretty much cleared up all the confusion for me.
It seems obvious now that some form items (textboxes, radiobuttons etc) automatically submit values back to the server and don't have to be included in the viewstate 'field'.
In spite of this I still have my reservations about viewstate's implementation.
The page I was having trouble with now seems more complicated than it need be.
It's a simple data entry page where a new record is to be inputted, stored in the database, and the page reloaded, greeting the user with a blank form and a table (gridview in this case) which displays the new record. With asp this is default
behaviour(well it can't be any other way of course!). With asp.net/viewstate the page postbacks, the form values are maintained and the gridview doesn't show the new record.
What I now need to do (after submitting the form and 'postbacking') is explicitly declare each of the form values to set them up with blank values, and disable viewstate on the gridview table so that the new record is displayed.
The workaround for this is to trigger a response.redirect back to the page but this seems a little 'inelegant'.
I can see the usefulness of viewstate in many situations but for data-entry that involves INSERTS it seems illogical. Why would you want this behavior? Am I missing something here?
mister_norto...
Member
32 Points
44 Posts
Viewstate preserved despite being turned off
Jan 28, 2011 01:16 PM|LINK
I am having trouble turning off viewstate for the controls in a page/form. Whatever I try the controls preserve & display the user input. I first turned viewstate off at control level, then page level and finally site level,yet still the form repopulates with values entered.
CONTROL
<asp:TextBox ID="TextBox1" runat="server" EnableViewState="False"></asp:TextBox>
PAGE
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="test.aspx.cs" Inherits="test" EnableViewState="False" %>
SITE
<pages enableViewState="false"/>
Inspite of all this the textbox, checkboxes, dropdowns etc preserve and display the user input on postback. Obviously I am doing something 'dumb' or laboring under a misconception. I'd like to know what!
Incidentally a gridview on the same page which displays all records from the table the form is used to fill can be disabled, ensuring it shows all records + the new one).
cyberbud
Contributor
3298 Points
551 Posts
Re: Viewstate preserved despite being turned off
Jan 28, 2011 01:44 PM|LINK
Hello mister_norton,
I might be wrong but you seem to be having completely wrong idea on viewstate.
Viewstate is used by web controls to preserve their states/properties. Like the cssclass property of your textbox, not it's values.
But when your page postbacks, what asp.net does is to fill the values in your controls from the form that has been posted. I hope you know what form is. form is the collection of key value pair values from your web page when posted back to the server. Asp.net simply maps the control's name in the form collection and set's its value
Correct me if I'm wrong
I hope I've been helpful
MCP(.net 3.5)
Nepal
ishwor.cyberbudsonline.com
dr.maqk
Participant
974 Points
235 Posts
Re: Viewstate preserved despite being turned off
Jan 28, 2011 01:44 PM|LINK
This is coming from ControlState
study this in detail: http://forums.asp.net/p/1599028/4063584.aspx
Mark as
My .Net Blog: DotNetExperience
mister_norto...
Member
32 Points
44 Posts
Re: Viewstate preserved despite being turned off
Jan 28, 2011 04:57 PM|LINK
Thanks for the responses. I had the wrong idea about viewstate, that's for sure.
How do I best prevent submitted values from being preserved upon postback.
Is it a case of resetting each property to it's default value in the code behind?
Textbox1.Text = ""; etc...
regards, Guy
nilsan
All-Star
16892 Points
3707 Posts
Re: Viewstate preserved despite being turned off
Jan 28, 2011 05:06 PM|LINK
I would suggest that you go through this article. I hope it will clear all your misconception.
Blog | Get your forum question answered | Microsoft Community Contributor 2011
mister_norto...
Member
32 Points
44 Posts
Re: Viewstate preserved despite being turned off
Feb 01, 2011 01:01 PM|LINK
Hi,
Great article nilsan! It pretty much cleared up all the confusion for me.
It seems obvious now that some form items (textboxes, radiobuttons etc) automatically submit values back to the server and don't have to be included in the viewstate 'field'.
In spite of this I still have my reservations about viewstate's implementation.
The page I was having trouble with now seems more complicated than it need be.
It's a simple data entry page where a new record is to be inputted, stored in the database, and the page reloaded, greeting the user with a blank form and a table (gridview in this case) which displays the new record. With asp this is default behaviour(well it can't be any other way of course!). With asp.net/viewstate the page postbacks, the form values are maintained and the gridview doesn't show the new record.
What I now need to do (after submitting the form and 'postbacking') is explicitly declare each of the form values to set them up with blank values, and disable viewstate on the gridview table so that the new record is displayed.
The workaround for this is to trigger a response.redirect back to the page but this seems a little 'inelegant'.
I can see the usefulness of viewstate in many situations but for data-entry that involves INSERTS it seems illogical. Why would you want this behavior? Am I missing something here?