How do I show/hide Label/Textbox when a RadioButton is clicked?

J

Jason

I am new to WebForms and am far more comfortable with WinForms. I am
wondering how simple things such as showing/hiding a label and textbox can
be accomplished if a certain Radiobutton option is clicked/unclicked without
taking a trip back to the webserver? Is there some easy way to do this? (I'm
gussing it will require JavaScript)
 
W

William F. Robertson, Jr.

Javascript is the route to take.

I would wire to the "onclick" event of the radio button.

The javascript I use to show and hide controls is:

document.all["tblShowMe"].style.display = "";
document.all["tblHideMe"].style.display = "none";

If you need more info, let me know.

HTH,

bill


tblReplacementInvoice.style.display = "none"
 
K

Karl

Without going back to the server it does require javascript, something
untested like:


<asp:label id="usernameLabel" runat="server" /> <asp:dropdownlist id="ddl"
runat="server" onChange="changed(this);" />


<script language="javascript">
function changed(ddl){
var username = document.getElementById('<%=usernameLabel.UniqueId%>');
switch (ddl.options[ddl.selectedIndex].value){
case "1":
username.style.visibility = 'hidden';
break;
default:
username.style.visibility = 'visible';
break;
}
}

</script>

That's just a basic example,
Karl
 
J

Jason

I also discovered that if I use the RadioButtonList, set
AutoPostBack="True", and hook the SelectedIndexChanged event I can
hide/unhide labels and textboxes using C# rather than JavaScript.

However, it is obvious it would be much more efficient to use JavaScript for
something like this... so I guess I better learn it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top