Neil Surridge
Neil Surridge

Reputation: 68

Setting style.display from asp.net c#

I am using a Panel in an ASP.NET webpage to hide or display a selection of control in response to a client side button click. Simple script toggles the visibility

<script>
    function SetToAddSurvey() {

    var x = document.getElementById("NewSurveyPanel");
    if (x.style.display == "none") {
        x.style.display = "block";
    } else {
        x.style.display = "none";
    }
}
</script>

I now need to toggle the display property on the server side following a database transaction. I know I can't use the code

NewSurveyPanel.visible = false; 

as it will cause the control to not be rendered and the above jscript to fail when it is called next.

NewSurveyPanel.Attributes["display"] = "block";

also doesn't work.

Is there an easy solution for this?

Ta.

Upvotes: 3

Views: 15742

Answers (3)

System Down
System Down

Reputation: 6260

Try this

NewSurveyPanel.Attributes["style"] = "display: none";

or

NewSurveyPanel.Attributes["style"] = "visibility: hidden";

What this does is to render the opening tag like this:

<div ....... style="display: none" ....>

Upvotes: 5

Win
Win

Reputation: 62260

Code Behind

NewSurveyPanel.Attributes["style"] = "display: block";

ASPX

<asp:Panel ID="NewSurveyPanel" runat="server">
    test
</asp:Panel>
<asp:Button runat="server" OnClientClick="SetToAddSurvey(); return false;" />
<script>
    function SetToAddSurvey() {

        var x = document.getElementById("<%= NewSurveyPanel.ClientID%>");
        alert(x);
        if (x.style.display == "none") {
            x.style.display = "block";
        } else {
            x.style.display = "none";
        }
    }
</script>

Upvotes: 0

Marcus Vinicius
Marcus Vinicius

Reputation: 1908

Use a CSS class:

.hidden {
   display: none;
}

....

 NewSurveyPanel.CssClass = "hidden";

Upvotes: 2

Related Questions