开发者

Unhide ASP Textbox control in Javascript Function

开发者 https://www.devze.com 2023-01-14 16:38 出处:网络
I have the following control: <asp:TextBox ID=\"textbox1\" runat=\"server\" Width=\"95px\" MaxLength=\"6\" />

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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消