Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you set the "Visible" property of a ASP.NET control from a Javascript function?

Bascially I want to know the best way to hide/show an ASP.NET control from a Javascript function. I figured I would just access the control in Javascript using:

var theControl = document.getElementById("txtEditBox");

Then just set the control's Visible property to true/false. It doesn't seem to be working, I can't seem to figure out how to set "Visible" to true/false. How can I do that? Also, is that the best way to hide/show a ASP.NET control from a Javascript function?

Thanks, Jeff

like image 996
Yttrium Avatar asked Nov 05 '08 23:11

Yttrium


People also ask

How do you make a control visible in Javascript?

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.

How to set panel visible true in JavaScript?

Setting Visible="false" makes the panel not to render in the generated HTML. If you want to make it show/hide in the client side, you need to make it Visible="true" and use a CSS class/in the style attribute having "display" property with the value "block" or "none" as required.

What are control properties in asp net?

User Control properties are used to set the values of a User Control from the parent page. For more details of how to create a User Control please refer to my previous article: Creating User Control in ASP.NET.


1 Answers

The "Visible" property of an ASP.NET control determines whether or not it will be rendered on the client (i.e. sent to the client). If it is false when the page is rendered, it will never arrive at the client.

So, you cannot, technically, set that property of the control.

That said, if the control is rendered on the client because the Visible property is true when the page is rendered, you can then hide it using javascript like this:

var theControl = document.getElementById("txtEditBox");
theControl.style.display = "none";

// to show it again:
theControl.style.display = "";

That assumes that the control's id attribute really is "txtEditBox" on the client and that it is already visible.

Also, is that the best way to hide/show a ASP.NET control from a Javascript function?

There is not necessarily a "best" way, although one better approach is to use CSS class definitions:

.invisible { display: none; }

When you want to hide something, dynamically apply that class to the element; when you want to show it again, remove it. Note, I believe this will only work for elements whose display value starts off as block.

like image 91
Jason Bunting Avatar answered Sep 26 '22 08:09

Jason Bunting