Urbycoz
Urbycoz

Reputation: 7421

Keep div visible on postback

I have a div which needs to be hidden when the page first loads. If the user then clicks the "btnShowDiv" button, it should then become visible.

The problem I have is that, once it has become visible I need it to stay visible. At the moment, whenever I click on an asp button the div reloads and becomes hidden again.

<DIV id="myDiv" style="display:none;">
   <label>some text</label>
</DIV>

<input type=button id="btnShowDiv" onClick="document.getElementById('myDiv').style.display='block';"> 

<asp:button id="btnASPButton"></asp:button>

Upvotes: 1

Views: 3526

Answers (2)

RameshVel
RameshVel

Reputation: 65867

Because asp button click causes the Postback to server, and reloading the page again. So the div goes back to initial state.

have a hidden field indicator like isDivVisible and turn it to 1 on first visibling the div.

and check this state on form load again to set the div visible.

Upvotes: 1

Chris
Chris

Reputation: 4471

You could use an asp:panel (renders as a div) instead of a div and handle the Visible property in your click handler.

Upvotes: 1

Related Questions