I have a user control on a page that should save state in a viewstate. Whenever a postback occurs, the entries in the view are visible, null.
Page
<%@ Page Title="" Language="C#" MasterPageFile="~/Main.master" %> <%@ Register TagPrefix="JR" TagName="JournalRanking" Src="~/Controls/JournalRankRadioButton.ascx" %> <script runat="server"> </script> <asp:Content ID="Content3" ContentPlaceHolderID="Content1placeholder" Runat="Server"> <asp:Panel CssClass="insetBG1" ID="FormView1" runat="server"> <JR:JournalRanking ID="JournalRanking1" runat="server" ViewStateMode="Inherit" /> </asp:Panel> </asp:Content>
User control
<%@ Control Language="C#" ClassName="JournalRankRadioButton" %> <script runat="server"> public String Test { get { if (ViewState["Test"] == null) { ViewState["Test"] = String.Empty; } return ViewState["Test"].ToString(); } set { ViewState["Test"] = value; } } public void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { this.Test = "Test"; } } </script> <asp:CheckBox runat="server" AutoPostBack="true" />
When I load the page, ViewState ["Test"] gets assigned to "Test", but when I check the box, the page performs a postback, and ViewState ["Test"] is again null. What am I missing?
-Update -
So, although I set EnableViewState = true on the page, and the EnableViewState control was false on the main page. I had to add
this.Page.Master.EnableViewState = true;
to the Office to make it work.
Thanks for the help!
source share