最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - Making A Control Visible Through Javascript - Stack Overflow

programmeradmin5浏览0评论

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
Add a ment  | 

1 Answer 1

Reset to default 5

First, 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
发布评论

评论列表(0)

  1. 暂无评论