Hi,
I have several Div tags on a page which are dynamic, i.e. depending on certain criteria they are either visible to the user or not. I want to add them to the page's view state so that upon postback they are not hidden again, how do I do this?
Hi,
I have several Div tags on a page which are dynamic, i.e. depending on certain criteria they are either visible to the user or not. I want to add them to the page's view state so that upon postback they are not hidden again, how do I do this?
ViewState["divAVisible"] = true;
ViewState["divBVisible"] = false;
Then within Page_Load:
if (ViewState.ContainsKey("divAVisible"))
divA.Visible = ViewState["divAvisible"]
...
The divA is defined as Panel
Alternatively, you can put something like:
<div id="divA" runat="server">...</div>
in your aspx and then it will become an instance of HtmlControl generated by VS.
From what I understand from the question it would seem you want to mark the DIVs as server controls. So declaring your divs with runat="server".
<div id="testpanel" runat="server"></div>
Or alternatively you could use an asp:panel. Then when you hit Page_Load of your page you can test for the postback and change the visibility accordingly.
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
testpanel.Visible = true;
}
}
But then again, if you don't want it as a server control this solution wouldn't work.
I would just use ASP.NET panels instead of divs if you are going the viewstate route. They render as div's so they would be exactly what you want.