如何从代码隐藏动态添加的div [英] How to hide dynamically added div from code behind

查看:95
本文介绍了如何从代码隐藏动态添加的div的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我点击登录按钮时,屏幕颜色显示黑色为黑色,另一个弹出窗口显示登录。登录弹出窗口包含一个关闭按钮。

i想要隐藏打开的弹出窗口或div关闭按钮点击后面的代码

thaks for help

when i click on login button then a div is display in the screen color in black and one other popup is display for login. Login popup contain one close button.
i want to hide that opened popup or div on that close button click on code behind side
thaks for help

推荐答案

你可以使用c#代码隐藏你的div。



U can hide your div using c# code.

<div id="div1" runat="server"> 
//put runat="server" in the div otherwise u can't get the div id in code behind.</div>





//代码落后





//in code behind

div1.visible = false // hiding the div

div1.visible = true // showing the div


其他解决方案在设置<$时是正确的c $ c> runat = server 然后从服务器隐藏div。但是你打算隐藏的div可以包含可以在你的脚本中某处引用的元素,如果它们不在页面上,你的javascript将抛出空引用错误。



为避免这种情况,请使用以下代码:



The other solutions are correct in setting runat=server and then hide the div from the server. But the div you intend to hide can contain elements which may be referenced somewhere in your script and your javascript will throw null reference error if they are not there on the page.

To avoid that use the following code:

yourDiv.Style.Add(HtmlTextWriterStyle.Display, "none");



设置显示 css属性为隐藏div而不是不完全添加元素。



这种方法更简洁,更安全,只需将可见性设置为false。


This sets Display css property to none to hide the div instead of not adding the element entirely.

This approach is much cleaner and safe then just setting visibility to false.




所有其他控件


all other controls





我相信所有ASP.NET WebForms控件的基类都是HtmlControl,它有一个Visible属性。



sample-div.visible = false;



I believe the base class for all ASP.NET WebForms controls is HtmlControl which has a Visible property.

sample-div.visible = false;


这篇关于如何从代码隐藏动态添加的div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆