Before looking at the code, Read about – What is JumpStart?
<%@ Page Language="C#" EnableViewState="true"%> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server"> /* ViewStateMode - * 1) It is a new feature in ASP.Net 4.0. Using this we have explicit control on the controls * which participate in viewstate, thereby preventing unnecessary information going into * viewstate during postbacks. * 2) In previous versions of ASP.Net, you can disabled viewstate for complete page by setting * EnableViewState="false" in @Page directive, the same can be used in ASP.Net 4.0, but for * more robost control on viewstate we got to use ViewStateMode. * 3) Once we disable viewstate for complete page using EnableViewState, we cannot enable it for * any other control used in that page, but using ViewStateMode you can achieve the same. * 4) You can disable viewstate of the page by setting ViewStateMode="Disabled", and still you can * enable viewstate for child controls using theie ViewStateMode="Enabled". * 5) ViewStateMode can have three values - Enabled, Disabled, Inherit. First two values are self * explanatory, Inherit means the control will inherit its ViewStateMode from its container. * 6) The default ViewStateMode of Page is Enabled and for that of a control is Inherit. * 7) ViewStateMode can be used in @Page directive too. *One must be careful in implementing this feature, as it may result unwanted results if it is * not used in appropriate ways. */ protected void Page_Load(object sender, EventArgs e) { Page.ViewStateMode = System.Web.UI.ViewStateMode.Disabled; if (!IsPostBack) { Label1.Text = "John"; Label2.Text = "Chris"; Label3.Text = "Bob"; Label4.Text = "Adam"; Label5.Text = "Scott"; } } </script> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <!-- Label Will not hold its value after postback --> <asp:Label ID="Label1" ViewStateMode="Disabled" runat="server" Text="Label1"></asp:Label> <!-- Label Will hold its value after postback --> <asp:Label ID="Label2" ViewStateMode="Enabled" runat="server" Text="Label2"></asp:Label> <!-- Label Will not hold its value after postback, as its container's viewstate is disabled --> <asp:PlaceHolder ID="PlaceHolder1" ViewStateMode="Disabled" runat="server"> <asp:Label ID="Label3" ViewStateMode="Inherit" runat="server" Text="Label3"></asp:Label> </asp:PlaceHolder> <!-- Label Will hold its value after postback, as its container's viewstate is Enabled --> <asp:PlaceHolder ID="PlaceHolder2" ViewStateMode="Enabled" runat="server"> <asp:Label ID="Label4" ViewStateMode="Inherit" runat="server" Text="Label4"></asp:Label> </asp:PlaceHolder> <!-- Label Will hold its value after postback, even though its container's viewstate is disabled, as label's viewstate in enabled --> <asp:PlaceHolder ID="PlaceHolder3" ViewStateMode="Disabled" runat="server"> <asp:Label ID="Label5" ViewStateMode="Enabled" runat="server" Text="Label5"></asp:Label> </asp:PlaceHolder> <asp:Button ID="Button1" runat="server" Text="PostBack" /> </div> </form> </body> </html>
OUTPUT –
Before Postback - After PostBack –







great help in understanding ViewStateMode