javascript 从asp.net c#设置style.display

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15773744/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 02:03:32  来源:igfitidea点击:

Setting style.display from asp.net c#

c#javascriptasp.net

提问by Neil Surridge

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

我在 ASP.NET 网页中使用面板来隐藏或显示控件选择以响应客户端按钮单击。简单的脚本切换可见性

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

因为它会导致控件无法呈现并且上面的 jscript 在下一次调用时失败。

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

also doesn't work.

也不起作用。

Is there an easy solution for this?

有没有简单的解决方案?

Ta.

塔。

采纳答案by Marcus Vinicius

Use a CSS class:

使用 CSS 类:

.hidden {
   display: none;
}

....

....

 NewSurveyPanel.CssClass = "hidden";

回答by System Down

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

回答by Win

Code Behind

背后的代码

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

ASPX

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>