C# 如何隐藏或显示 div

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10902959/
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-08-09 15:42:24  来源:igfitidea点击:

How to hide or show a div

c#javascriptasp.net

提问by user1416156

I have on my Page_Load registered this

我在我的 Page_Load 上注册了这个

 Page.ClientScript.RegisterStartupScript(this.GetType(), "clientscript", "document.getElementById('showdiv').style.visibility = 'hidden';", true);

But its not getting hidden... My div is as shown below

但它没有被隐藏......我的div如下所示

<div id="showdiv">
   <input class="button" type="button" value="OK" name="success_button" id="my button"  onclick="javascript:window.close();" />
   </div>

what am I doing wrong?. Thank you for your help

我究竟做错了什么?。感谢您的帮助

采纳答案by atconway

I highly recommend doing this simple client side manipulation (show/hode rows controls, etc.) with JavaScript or even more easily with a .js library like jQuery. After you include the jQuery scripts in your application, this is all you need to do to have that DIV be hidden after the page has completed its initialization.

我强烈建议使用 JavaScript 或使用 .js 库(如 jQuery)更轻松地进行这种简单的客户端操作(显示/隐藏行控件等)。在您的应用程序中包含 jQuery 脚本之后,您只需要做的就是在页面完成初始化后隐藏 DIV。

Include this script section at the top of your page or in a referenced .js file if you already have one:

将此脚本部分包含在页面顶部或引用的 .js 文件中(如果您已经有了):

<script type="text/javascript">

 //$(document).ready is used to define a function that is guaranteed to be called only after the DOM has been initialized.
 $(document).ready(function () {
    //Hide the div
    $('#showdiv').hide();
    //conversely do the following to show it again if needed later
    //$('#showdiv').show();
 });

</script>

jQuery API documentation on this method:
http://api.jquery.com/hide/

关于此方法的 jQuery API 文档:http:
//api.jquery.com/hide/

回答by Jeremy Wiggins

Why not use an asp:Panelserver tag?

为什么不使用asp:Panel服务器标签?

Front End:

前端:

<asp:Panel runat="server" ID="ShowDiv">
...
</asp:Panel>

Back End:

后端:

ShowDiv.Visible = false;

The Panelcontrol will be rendered as a <div>at runtime. This seems cleaner to me than registering a client script.

Panel控件将<div>在运行时呈现为。这对我来说似乎比注册客户端脚本更清晰。

回答by jatrim

I think this is another load order problem.

我认为这是另一个加载顺序问题。

Your script runs as soon as it's loaded. If the page element it's trying to hide isn't loaded into the DOM at the time the script runs, then there's nothing to hide. I believe the registered scripts all go into the top of the page, before the HTML content, so this will always happen.

您的脚本一加载就运行。如果它试图隐藏的页面元素在脚本运行时没有加载到 DOM 中,那么就没有什么可隐藏的了。我相信注册的脚本都会在 HTML 内容之前进入页面顶部,所以这总是会发生的。

To get this to work, you would have to put it in a load event listener. See: Running javascript code on page load, without using the onload tag

为了让它工作,你必须把它放在一个加载事件监听器中。请参阅:在页面加载时运行 javascript 代码,而不使用 onload 标记

That said, since you're trying to hide the page element, without condition, you'd probably be just as happy turning this page element off at the server side, either by added a class to the element that your CSS makes hidden, or manipulating its style/visibility directly from the server code.

也就是说,由于您试图无条件地隐藏页面元素,您可能会很高兴在服务器端关闭此页面元素,方法是向 CSS 隐藏的元素添加一个类,或者直接从服务器代码操纵其样式/可见性。

If there is supposed to be some condition on whether or not the Div is visible, then doing it all in client-side javascript is probably better, so that you don't have to make a server trip just to control visibility.

如果应该有一些关于 Div 是否可见的条件,那么在客户端 javascript 中完成所有操作可能会更好,这样您就不必为了控制可见性而进行服务器旅行。

回答by ebram khalil

you have 2 options

你有 2 个选择

1-Add "runat=server" attribute to your div then from code behind access it and make visibility false or add style to make it invisible.

1-将“runat=server”属性添加到您的 div,然后从后面的代码访问它并使可见性为 false 或添加样式以使其不可见。

myDiv.Style.Add("display","none");

2-Add javascript function to hide it and you could use jquery to do it.

2-添加javascript函数来隐藏它,你可以使用jquery来做到这一点。

回答by dLcreations

Javascript:

Javascript:

$(document).ready(function(){

    $(".slidingDiv").hide();
    $(".show_hide").show();

$('.show_hide').click(function(){
$(".slidingDiv").slideToggle();
});

});

HTML

HTML

<a href="#" class="show_hide">Show/hide</a>
<div class="slidingDiv">
Fill this space with really interesting content. 
<a href="#" class="show_hide">hide</a>
</div>

CSS

CSS

.slidingDiv {
height:300px;
background-color: #99CCFF;
padding:20px;
margin-top:10px;
border-bottom:5px solid #3399FF;
}

.show_hide {
display:none;
}

回答by Bruce Graves

I came here looking for a solution and ended up adding the "runat=server" to my div then hiding it in code-behind with

我来到这里寻找解决方案,最终将“runat=server”添加到我的 div 中,然后将其隐藏在代码隐藏中

myDivID.Visible = false;

myDivID.Visible = false;

回答by Ahmed Abaza

HTML

HTML

 <div id="div1" runat="server"></div>

C#

C#

div1.Visible=false;

I think this will work ...

我认为这会奏效...

回答by Bennani HAMZA

we create link calling javascript

我们创建链接调用 javascript

  <p><a href="#" class="show_hide" onclick="recoverDiv();">Mot de passe oublié</a></p>

the div to show and hide

要显示和隐藏的 div

<div id="divdiv"  runat="server" class="HideMe">
</div>

and in javascript add:

并在javascript中添加:

<script type="text/javascript">

    function recoverDiv() {
        $('#divdiv').attr('class', 'ShowMeForced');
    }
</script>

and in css:

在 css 中:

<style type="text/css">
    .HideMe { display:none; }
    .ShowMe { display:block; }
    .HideMeForced { display:none !important; }
    .ShowMeForced { display:block !important; }
</style>