Marthinus Elliott
Marthinus Elliott

Reputation: 67

"Visible" property between c# and javascript

Firstly apologies for the strange title. Couldn't think of one lol.

When i hide an element in code behind (c#), for example:

btnRemoveAvail.Visible = false;

then the following jquery line wont work (when the time comes to unhide):

$('#btnRemoveAvail').show();

(it returns no error at all, as if its 100% cool.. but nothing happens)

I tried then the following (as the jquery forum says .show is similar to setting display to block..?): document.getElementById("btnRemoveAvail").style.display = "block";

And that returned an error: document.getElementById("btnRemoveAvail") is null

I am sure its probably something simple that i am completely misunderstanding between code behind and HTML..

Upvotes: 0

Views: 3648

Answers (3)

Nancy
Nancy

Reputation: 1

I had this issue today. I ended up doing the following.

HTML

<div id="regularFlow" runat="server" style="display:none;">

jQuery

$('#<%=regularFlow.ClientID%>').show();

C#

regularFlow.Style.Add("display", "block");
...
regularFlow.Style.Add("display", "none");

Upvotes: 0

scartag
scartag

Reputation: 17680

In webforms (i'm assuming you are using webforms) when you set a controls visibility to false it doesn't get rendered as html at all, so it's not available in the DOM for jquery to manipulate.

if you want it to be available you can set its client side attributes from code behind like this.

 btnRemoveAvail.Attributes.Add("style","display:none");

You can now manipulate its visibility or any other property via jquery since it will be available in the DOM but just not visible.

Hope this helps.

Upvotes: 5

agarcian
agarcian

Reputation: 3965

Can you check that there is an element created in your page named btnRemoveAvail?

I bet it is not being created. I believe that setting the button as Visible = false from the server side simply doesn't render the object on the client.

What you need to do is to set the style of the button so it includes a display:none. That way the element will be rendered but hidden via css. Then you can make a call to the .show method and it should work.

Upvotes: 2

Related Questions