views:

4361

answers:

8

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

+8  A: 

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.

Jason Bunting
What are the other options for "style.display"? How do I show it again?
Yttrium
I updated with the answer...
Jason Bunting
Why did someone downvote this?
Jason Bunting
No clue... but thanks for pointing out the insane duplication... I've deleted my copy :P
Timothy Khouri
Sure thing - some people are such reputation whores that they just say the same things everyone else says hoping to gain something from it, when it really just clutters the page up. I don't mind honest mistakes, but some people do it just for rep.
Jason Bunting
A: 

instead of using visible, set its css to display:none

//css:
.invisible { display:none; }

//C#
txtEditBox.CssClass = 'invisible';
txtEditBox.CssClass = ''; // visible again

//javascript
document.getElementById('txtEditBox').className = 'invisible'
document.getElementById('txtEditBox').className = ''
Jimmy
A: 

This should hide the control:

theControl.style.display = 'none';
gfrizzle
What is this adding? Everyone else already repeated this, ad nauseum. This just adds more clutter to the page.
Jason Bunting
Not to mention, it doesn't even really answer all of his questions. Try formulating a longer, more thought-out response next time instead of a one-off hoping for reputation points.
Jason Bunting
Oh, you are a VB guy...that explains it. :P
Jason Bunting
Nothing like discouraging participation. Notice that in the time it took to fire off what I was hoping was a helpful clue in the right direction, a bunch of other people did the same. Next time I'll be sure to allow you to properly moderate all answers before trying to be helpful.
gfrizzle
A: 

Set the style to "display: none".

var theControl = document.getElementById("<%= txtEditBox.ClientID %>");
theControl.style.display = "none";
Michael Kniskern
That "txtEditBox.ClientID" is an important touch -- if it's runat="server", you'll need it every time. +1
ojrac
You don't need it every time - you only need it if the control is not directly in a page that has no master page. If it is on a simple ASPX page, the ID will remain the same.
Jason Bunting
You are right... I am used to always using ASP.NET Master pages...+1
Michael Kniskern
UserControls and Templated Controls also need you to use Control.ClientID. A Panel will add to the generated ID
Slace
Exactly - most people are writing non-trivial pages, and so your answer is worth noting.
Jason Bunting
A: 

You can use the display property for this. But as Jason noted, this is a DHTML DOM (client-side) property that is completely independent from the ASP.NET (server-side) Visible property which controls rendering.

theControl.style.display = "none";

Display Property

C. Dragon 76
A: 

You want to set the display style property to 'none' (to hide) or null to show.

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

   theControl.style.display = 'none';

   theControl.style.display = null;

Doing it the jQuery way:

   $('#txtEditBox').hide();

   $('#txtEditBox').show();
tvanfosson
A: 

You can't hide/ show the ASP.NET version of the control as that only exists in a server context. To use JavaScript you need to play with the controls style/ visibility state.

The only kind-of way to do it would be to wrap the control in an UpdatePanel and have something like this:

<asp:UpdatePanel ID="panel" runat="server">
  <ContentTemplate>
    <asp:TextBox ID="myTextBox" runat="server" />
  </ContentTemplate>
  <Triggers>
    <asp:AsynchronousPostbackTrigger ControlID="button" EventName="Click" />
  </Triggers>
</asp:UpdatePanel>
<asp:Button ID="button" runat="server" OnClick="toggle" Text="Click!" />

Then you need this in your code behind:

protected void toggle(object sender, EventArgs e){
  myTextBox.Visibility = !myTextBox.Visibility;
}

Now when you click the button an async postback occurs and it will refresh the UpdatePanel.

Note: This is not a good solution, as it'll be a very heavy AJAX request, because you need to submit the ViewState.

Also, it may not be 100% right, I did that from memory.

Slace
Holy crap - talk about going the long way home!
Jason Bunting
Hey, never said it was a GOOD solution, just an entirely .NET solution ;)If you're not going to use display = "none" then you're a fool!I just saw that every one of those solutions had been downvoted, thought maybe a pure ASP.NET solution was wanted :P
Slace
The scary thing is, you and I know this isn't really a good way to do it, but I am willing to bet someone will see your solution and think "Hey, that is a great way to do it - no JavaScript!!!" - those people scare me. :)
Jason Bunting
And then we submit to the daily wtf!I should put that code on my blog as an example of what not to do.
Slace
I would concur - sometimes it is nice to have a bad example. Heck, I am sure some of my code from last week could serve as a bad example. Continually learning...
Jason Bunting
A: 

Hi, i think Using css is best solution for it.