tags:

views:

305

answers:

3

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?

+1  A: 
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.

xandy
I tried that: <div id="divA" runat="server">...</div> and I get a parser error "server tag not well formed" which also contradicts what it said in my asp.net 2.0 book!
flavour404
This is my actual div:<div id="<%# Eval("PublicationID") %>" style="display: none; position: relative;">I also tried <div id="<%# Eval(PublicationID') %>" style="display: none; position: relative;">but each combination of "" or '' seems to throw and error when combined with runat="server."
flavour404
http://www.issociate.de/board/post/274932/%3Cdiv_id=%22help%22_runat=%22server%22%3E%3C/div%3E.html I read some of similar situation on net and most referring to similar problems, please take a look and as I said in first place, divA can also be a Panel, so if Panel isn't a problem to you, use that.
xandy
+1  A: 

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.

Shane Kenney
+3  A: 

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.

Kelsey
+1 my thoughts exactly, no point reinventing something thats already there.
Brendan Kowitz