How to hide server control in JavaScript
Dear All, I want to hide server control in JavaScript. I do not want to use "none". Are there any oth开发者_StackOverflower ways to hide the control?
It is not possible to hide server controls in JavaScript. JavaScript is executed client-side, while server controls are processed server-side. JavaScript and server controls never ever communicate with each other, it is not possible.
If you would like to hide the server control, you should edit the HTML file (I don't know if this is your .aspx, .php or whatever, but I hope you understand what I mean.)
You can hide only HTML output generated by server-side control:
Markup:
<uc:MyControl runat="server" ID="myControl" ClientIDMode="static">
...
</uc:MyControl>
JavaScript (jQuery):
$('.myControl').hide(); // .show();
You can hide the server control after it's been rendered to the page. An alternative to display:none
is visibility:hidden
;
You could use visibility
instead:
var obj = GetServerControlById(someId);
obj.style.visibility = "hidden"; // Hides
obj.style.visibility = "visible"; // Shows
If you're using ASP.Net, you can get the control via it's ClientId
property:
var clientId = "<%= yourServerControl.ClientID %>";
var obj = document.getElementById(clientId );
精彩评论