Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When is safe to disable viewstate?

When is safe to disable viewstate? For which controls? Under what circumstances?

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.

like image 734
eKek0 Avatar asked May 14 '09 04:05

eKek0


1 Answers

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.

like image 165
Jeremy Avatar answered Sep 19 '22 19:09

Jeremy