views:

30

answers:

2

I have the following control:

<asp:TextBox ID="textbox1" runat="server" Width="95px" MaxLength="6" />

which i would like to be hidden/invisible on page load, and have the textbox appear after clicking a button/running some javascript without reloading the page.

Here's my current button:

<asp:Button ID="cmdShowBox" runat="server" Text="Show Button" onclick="showBox(); return false"  />

and lastly here's my current javascript function:

                function showBox() {
                var theControl = document.getElementById("<%= textbox1.ClientID %>");
                theControl.style.display = "none";


                }

I was starting with just showing the box on load, then trying to click a button to make it hide, but I can't even get that to work :( When I run the code as it is above I get a server error which says

Compiler Error Message: BC30451: Name 'textbox1' is not declared.

Thanks for any help/advice.

+1  A: 

Hey,

If you are setting the server-side visible property to false, this doesn't render the control at all on the client. That's also why you can get the textbox1 error message. Instead do:

<asp:TextBox id="textbox1" ... style="display:none" />

And then this element is rendered, but hidden. Then this should work.

function showBox() {
    var theControl = document.getElementById("<%= textbox1.ClientID %>");
    theControl.style.display = "";
}

To show it.

Brian
It's a compiler error though, the getElementById is not returning null... are you sure this is the problem?
asawyer
@Brian there is no `style` property on a `TextBox`. He'd have to associate a css class.
KP
I got a compile error when trying the problem too, but it was a different one. I would accept Vinay's answer though as going with OnClientClick seems to be the answer to this problem.
Jay
"@Brian there is no style property on a TextBox. He'd have to associate a css class." That's the main problem I was running into when trying to make this work..
Albert
ok so i was able to apply a style="display:none" tag to the textbox, which worked. And then my only issue (which i didn't include in my orig post) was the textbox1.ClientID reference wasn't working because the control was in a formview control. So I referenced it like: var theControl = document.getElementById("formview1$textbox1"); and theControl.style.display = "inline"; which worked!! Thanks for your help.
Albert
Controls that have properties defined that don't match a server-side property automatically push these down to the client side HTML element. So style isn't a server-side property, it just renders it directly as is. Glad to see it worked. Yes formview and some other containers (where templates are involved) can cause additional headaches.
Brian
+4  A: 

onclick on a button is bound to a server side method. use a normal html button or Use OnClientClick on the asp:button. also setting visible false on the textbox on the server will not even rencer the control in the first place trying hiding it by setting a css with display none.

Vinay B R
You beat me to it.. +1
KP
i actually was using the OnClientClick, that was just a mistype when I was preparing this question. thanks for your post.
Albert