admin管理员组文章数量:1391955
I have a label and a div called "menu" that is currently invisible. I want that when the user clicks the label. It will make the div visible. I thought of doing it through javascript, how do I make a control visible through javascript?
I have a label and a div called "menu" that is currently invisible. I want that when the user clicks the label. It will make the div visible. I thought of doing it through javascript, how do I make a control visible through javascript?
Share Improve this question asked Jan 23, 2011 at 21:26 Or BetzalelOr Betzalel 2,61712 gold badges50 silver badges75 bronze badges 1- 1 If the control has a Visible="false" on the server side, the control is not rendered, thus not accessible by JavaScript. – Caspar Kleijne Commented Jan 23, 2011 at 21:31
1 Answer
Reset to default 5First, if you want to access controls on the client-side, they must be rendered as html. When you use Control.Visible it won't be rendered on the client and is only accessible on the serverside. Therefore you have to use CSS to toggle it's visibility on the clientside.
show the div:
document.getElementById('menu').style.display = 'inherit';
You could hide it with:
document.getElementById('menu').style.display = 'none';
You should keep in mind that the id of serverside-controls could change when it's inside of an other NamingContainer than the page(f.e. in a GridView or UserControl). So you should use Control.ClientID to get the correct ID that'll be generated from ASP.Net:
So this is better:
document.getElementById('<%= menu.ClientID %>').style.display = 'none';
In ASP.Net 4.0 it's possible to customize the ClientID. For further informations:
- http://www.thereforesystems./working-with-client-id-in-asp-net-4/
- http://msdn.microsoft./en-us/library/system.web.ui.control.clientid.aspx
本文标签: cMaking A Control Visible Through JavascriptStack Overflow
版权声明:本文标题:c# - Making A Control Visible Through Javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744745101a2622827.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论