问题
When is safe to disable viewstate? For wich controls? Under what circunstamces?
In a user control I have disabled viewstate, but if I attempt to click in this control
<asp:LinkButton ID="LinkButton1" runat="server"
CommandName="Delete"
OnClientClick="return confirm('¿Está seguro que desea eliminar el mensaje?');"
EnableViewState="true">
<asp:Image ID="ImageButton1" runat="server" ImageUrl="~/Content/Images/delete.png"
ToolTip="Eliminar mensaje" /> Eliminar
</asp:LinkButton>
I get an System.InvalidOperationException exception. It is inside a ListView.
回答1:
Most controls behave as you would expect with viewstate disabled. The more 'dynamic' controls with built in functionality like Gridviews tend not to play well without viewstate.
Are you sure that the exception is related to the viewstate given that you have the enableviewstate property set to true on your control?
回答2:
It boils down to whether or not you want the page to remember things across postbacks. If you are recreating or assigning values on each postback viewstate is not necessary
Here's a few good pointers
Dynamically inserted value on the controls (By binding or programmatically assigning) – The values of this controls will not retain when it is rerendered, e.g. Switching from view1 to view2. But you have to consider two things, if you think repopulating the values for every render is to heavy to implement then don’t disable the viewstate, if not then you may disable it and reinitialize your controls on render event. Why am I suggesting this? It’s because processing serverside code is much faster than transferring a large junk of data back to the server and unto the client on roundtrips.
On Datalist and DropDownList – If you are not using the OnSelectedIndex Change event then you may disable the viewstate.
On Gridviews – This is the hardest part to decide whether to disable viewstate or retain it. If you are just displaying data on it or even using it just for selection, then disable the viewstate. If you are using paging, edit or delete functionality then don’t. Gridview has the largest viewstate capacity so you should use it wisely. If you have to update as many as 5 columns then why not just open another view then set the values there to be updated rather than updating it on the gridview directly.
回答3:
You could always disable view state. If you do you have to find other ways to maintain state for the page if it is required. On way is to use the old-fashioned methods of hidden form fields.
As long as you are not putting large, complex objects in viewstate there is typically no reason to disable it.
回答4:
If you are using then .net framework 2.0 or higher version of the framework then you could use new feature called control state instead of view state. It very much faster in terms of performance as compared to the viewstate.
For more details please see below sites...
http://www.pluralsight.com/community/blogs/fritz/archive/2004/07/01/472.aspx
http://msdn.microsoft.com/en-us/magazine/cc163901.aspx
http://www.codeproject.com/KB/user-controls/TestControlStateEx.aspx
来源:https://stackoverflow.com/questions/861537/when-is-safe-to-disable-viewstate