admin管理员组

文章数量:1289877

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.

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.

Share Improve this question edited Apr 3, 2013 at 6:38 Cheran Shunmugavel 8,4591 gold badge35 silver badges40 bronze badges asked Apr 2, 2013 at 20:28 Neil SurridgeNeil Surridge 681 gold badge1 silver badge5 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 5

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" ....>

Use a CSS class:

.hidden {
   display: none;
}

....

 NewSurveyPanel.CssClass = "hidden";

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>

本文标签: javascriptSetting styledisplay from aspnet cStack Overflow