I am already storing the viewstate in session. However when I use updatepanel, I still see this:
|0|hiddenField|_EVENTTARGET||0|hiddenField|_EVENTARGUMENT||0|hiddenField|_LASTFOCUS||72|hiddenField|_VIEWSTATE|/wEPZwUPOGQwMTViZDAyYzljM2RiDkOzqPgrsrLq7oLjMrtEqvksOkereJ+iwT7JWm4o3eY=|1|hiddenField|_SCROLLPOSITIONX|0|1|hiddenField|_SCROLLPOSITIONY|0|2524|hiddenField|__EVENTVALIDATION|/wEdAHT9Fws9F+QU02vyHw9EtdeObLsgjLvX39DC4H8vBJFsyG18cELKaPXq+6Ahww6b1rU+8PzArOjCJUc2STBQ8LpyrHZEzuzo3X7y36pxjtFPwEmlgfvDnB/DNXrQtx2v9SFwA6EUH25J8efaI9V++QjV3oYJ3CPuCO4rknM0UDU/Giw7O8WNpkhgFBXVsNXctDwPxcu09HF7wSzZJLw+yDLL27KCQmMvwyeQidE5tg2IjJwVkoU7oeLVX9C9RJWJzOSAE0IYtgz1LUDFnEzz2hYOu5ayAaskN4ApdAYd5uRg6K90gthxiirKGv+NQoPCw4Y6j7rA2ev411USg9tq7NB9PWYHwL5OwnZ+EXR/NJSLlRHPRrtnXSvGItKU8f0Dp34XG1CYjecxpmI5DuS/QVB3TlY541cQjTzcCv/HsuxlIciiPNCUviC1rvOBSWAA5xRgeRoRdmrXcedawNvUNZNX9qlIl95cg1FFB3mx2hy79XB+FEw3T7oc//ZDu3Y2K2NaCCK9+JQB0ysMN3aBxKawLkwRYSkH8judvo71vrM731VSHTSByvJ+f9qJ+bkVEjIfqddGEVOgyU0snyHEF8GKyF3iWsXTzAe9Fc2I41sQQfNQ1Mb/i5HmDqpAsKH4S1tt96XgwmzKUAHVyC9YllJNz7KbvuAn9E45Xu+8y2sy4PL+ocjVp1LzZqKm1zxDydsyYEJykODvuiTL0ZFyUUlFqCLvWZs1Tv/gcwhV/SX1YwsxN6djvw+U8ixFqIj/WKymYeC+uRep3FgV5RZvwh3ZZWX+Uyqb4NWBQBMqlcEruJg27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KClBM3IGiMNVGZ5GAixwtnbQZwA5eerBurSYQ79Cp5UExRubalAdi7EIXdznejVWgTEmL6oF9xSVXPB/4B7/Odm4FcDGaCaD7uEjgTdyDCeVA/OW2e3hF+dPX92E9a1/ItywkONZRylQmQ5LUb5NE8LVejlFSluTqPTtcGUZ6E1fMIFtKja0pvPJGc+iRhDN2HMOIlGqBVGYhkX1NOBuedASsY0HLFXnvr2L/aU+kcXsjFrKM74NLrtGQzffpFomEjZOvHZ34GMwGxvjaj4+6FI/HQbQlCNJnpwmQGauXnffaIUBtOesA1gpJYB//Yy/J4tMSNePfi6S06BIWWH1lWdRtjzLndYgb/aBBtPpuERDmR13pBLUCNjFzYVhMteyaW6iNMyQ4vhimTHpfJmNetj1a8qL+AZ7mflGfEdlBhM5oPVUOYeqPQD+nAlHmqmGopXtSAYNb/zIUrYdEKXomNrF2x0gU3Z8CovtY31qvaFstoyKBokBco1V1RNAmbCIC7qcAV5rT+5znsrbWy9K+ltbeK18p5o22ntmveymm7IifzgKdI9Pw9NgqbV/e7Wvj2qjIRH7MVyItXHkfOjP24GZ8IFFCFcT9D/bi0kSfSe36Xk89p1eJrsuNNxL/QRqaq|0|asyncPostBackControlIDs|||0|postBackControlIDs|||102|updatePanelIDs||tctl00$Body$updIGrp,,tctl00$Body$updITyp,,tctl00$Body$updRpt,,tctl00$Body$updSrch,,tctl00$Body$updDoc,|0|childUpdatePanelIDs|||57|panelsToRefreshIDs||ctl00$Body$updRpt,,ctl00$Body$updSrch,,ctl00$Body$updDoc,|2|asyncPostBackTimeout||90|0|formAction|||39|scriptStartupBlock|ScriptContentNoTags|removeLastColumn('ctl00_Body_grvDocs');|208|scriptStartupBlock|ScriptContentNoTags|Sys.Application.add_init(function()
{ $create(Sys.UI._UpdateProgress,
{"associatedUpdatePanelId":"ctl00_Body_updRpt","displayAfter":0,"dynamicLayout":false},
null, null, $get("ctl00_Body_ctl07"));
Is there any way I can prevent this from being sent in response?
Extracted from stackoverflow:
The __VIEWSTATE field is also used to store control state, which is
not optional. Furthermore, the information contained in the view state
is used to validate the postback, if I'm not mistaken (and validation
is enabled, which is the default). So as long as you have the form
with runat="server", you'll have a viewstate field. However, you
should notice a much smaller field size if you disable all viewstate.
By the way, there are some tricks to remove the viewstate. Check the following post:
Override Render and strip ViewState out with a Regex.
Set the 'EnableViewState=false' on the UpdatePanel. This will disable the ViewState of all child controls contained within in. Or, if you need a more granular approach, you can disable the ViewState on a control by control basis too.
Related
I am confused in how to use ViewState in C#, for example what is the benefit of using:
ViewState["VST"]="Value1";
Lable1.Text= ViewState["VST"].ToString();
Whereas I can use:
string container= "Value1";
Lable1.Text= container;
Your ViewState consists of variables that are kept with the post-backs of your page, because they are sent to the client and the client sends them back with the entire page.
Hence, if you do:
string container= "Value1";
Lable1.Text= container;
Then the users sees the page and hits the submit button, your container string will not exist.
If however you uses the ViewState, ViewState["VST"] will still have the value as it will be "refreshed" when the user submits and sends the page back.
Read more here and also understand the ASP.NET page life cycle.
As per documentation:
View state is used automatically by the ASP.NET page framework to persist information that must be preserved between postbacks. This information includes any non-default values of controls.
You can also use view state to store application data that is specific to a page.
For details see a link:http://msdn.microsoft.com/en-us/library/bb386448(v=vs.100).aspx
If you want to persist the values after postback also than ViewState is the best option.
Every time your application do postbacks current values of your controls are being wiped out. So in order for you to store any values WITHIN THE PAGE you can save them in ViewState. Of course you must set the EnableViewState property first to true. Additional info, if you want to store any value or state while jumping into multiple pages you can use Session instead.
I have a user control, with viewstate enabled. i am using it on a page with viewstate disabled.How can I access viewsate info?
If you disable ViewState on the page, no rendered controls on that page will have ViewState information available.
If you want to disable ViewState just for some controls but not all, your best bet is ControlState as seen here.
As I know the ViewState property is a protected member of the Control class so it not accessible from other objects.
If you just write "ViewState" in a Page declaration, it is actually the member of that Page object which in your case is disabled.
However you should be able to reach your user control's ViewState within your User Control.
See this explanation:
http://msdn.microsoft.com/en-us/library/ms972976.aspx#viewstate_topic5
But anyway, this doesn't look like a good way of storing state information on a web page.
I am dynamically loading user controls to switch between views in my project. I am aware that I need to reload my user control on every postback. The odd thing is my viewstate for the control is maintained even though the control is gone? I confirm that the panel I loaded it into is empty and then I check the view state and my value is there.
What's stranger is that if I load a different control, it can see the viewstate from the previous control? I checked and my page cannot see viewstate from my dynamically loaded control and visa versa. This makes me think the user control is treated as its own page. The confusing part is why the second view I load can see values from the first and why the values are there even though I the control has disappeared?
I also find this section of the code to be useless. Why is it removing the control? The panel is always empty (the code is from Telerik):
string controlId = LatestLoadedControlName.Split('.')[0];
Control previousControl = pnlControlPlaceholder.FindControl(controlId);
if (!Object.Equals(previousControl, null))
{
this.pnlControlPlaceholder.Controls.Remove(previousControl);
}
I looked at several posts and most say that viewstate is lost on every postback, although this is not the case for me. Perhaps because I'm using update panels. Although if an intial request handles an event and then reloads the same control again, the viewstate is lost. It only seems to preserve the viewstate on the very next postback.
Can anyone explain this odd behavior of sharing viewstate between user controls or why it is there even though the control is lost?
Apparently you can read viewstate between pages in two scenarios... Cross page postback and when using Server.Transfer. I believe the cross page postback scenario would explain what I am seeing.
I am using AjaxToolkit:ReorderList Control in an asp.net page(C#) and changing value of a viewstate[""] on OnItemReorder event, when I click on any button on the same page the value of viewState is not founded.
Please provide me appropriate solutions.
Thanks...
As far as I understand it, the OnItemReorder event happens in something other than a regular postback. Thus, the page is not re-rendered, and consequently the changed ViewState doesn't get sent back to the client. As a result, when you do a real postback, you send back the original ViewState from before you made the change.
I got around this by storing the data I was trying to sort in a session variable rather than ViewState, but I'm not entirely happy with that.
I am having a problem in which two controls which are added programmatically are trying to load each other viewstate, I want to clear the viewstate before loading the controls, I tried Viewstate.Clear but it did nothing, when I disable viewstate on the container of my controls everything works fine except that the control's state is not kept. Is there a way to clear viewstate of just a specific control?
From your description, it would seem that you are making one of the common mistakes when loading your dynamic controls - either you are loading them too late or you are not assigning them unique IDs (and assigning them the same unique id each time a postback occurs).
If this is indeed your problem, then clearing the viewstate is not the appropriate action to be taking. It is quite simple to fix, check these three links:
http://msdn.microsoft.com/en-us/library/ms972976.aspx
https://web.archive.org/web/20210330142645/http://www.4guysfromrolla.com/articles/092904-1.aspx
http://geekswithblogs.net/shahed/archive/2008/06/26/123391.aspx
Yes ,
string controlName = "name of control";
ViewState[controlName] = null;
// create control, add it to the page
If ViewState gets in your way and you haven't done so already, please read
TRULY understanding ViewSate
It will make you much more comfortable in working with ViewState and the whole ASP.NET page lifecycle.
Assuming both controls inherit from System.Web.UI.Control. You can disable their individual ViewStates by setting their EnableViewState to false. Also you can clear their ViewState, as each control has a ViewState property.
Make sure the id's of the two programmatically added controls are different and the ViewState problem should go away.
You can disable viewstate on a specific control:
EnableViewState="False"