This free JavaScript article will show you how to add onclick events to the ASP.NET RadioButtonList controls quickly and easily.
- Demo
- Enlarge
- Reload
- New window
Free iPage Web Hosting for First Year NOW
If you're still looking for a reliable web host provider with affordable rates, why you don't take a little of time to try iPage, only with $1.89/month, included $500+ Free Extra Credits for the payment of 24 months ($45)?
Over 1,000,000+ existisng customers can not be wrong, definitely you're not, too! More important, when you register the web hosting at iPage through our link, we're going to be happy for resending a full refund to you. That's awesome! You should try iPage web hosting for FREE now! And contact us for anything you need to know about iPage.
I've been fighting to add javascript code to the onclick event of ASP.NET RadioButtonList controls for the last hour, and given the frustration it caused me, I am writing the solution up for future reference. The real world code I am writing is far more complex than this example, but hopefully this illustrates enough to act as a primer, and to jog my own memory in future.
It's actually quite simple. For the example, I'm going to make a RadioButtonList make a Textbox dynamically appear and disappear.
The ASP.NET RadioButton List Control
Here's how the RadioButtonList might look...
<asp:RadioButtonList ID="rblFoo" runat="server" RepeatLayout="Flow" RepeatDirection="Horizontal"> <asp:ListItem Text="Show" Value="show" /> <asp:ListItem Text="Hide" Value="hide" /> </asp:RadioButtonList> <asp:TextBox ID="tbFoo" runat="server"></asp:TextBox>
The Javascript Function
Here's a javascript function that we are going to attach to the onclick event of each radio button (you would typically put this in a javascript file, and reference it via a SCRIPT tag in your ASPX page)
function fooClick(option_value,tb_id){ var textbox_object = document.getElementById(tb_id); if (option_value == 'show'){ textbox_object.style.display = 'block'; } else { textbox_object.style.display = 'none'; } }
Attaching the Javascript to the OnClick event of the ASP.NET RadioButtonList Options
In the OnLoad() event of your page, do the following;
foreach(ListItem li in rblFoo.Items){ li.Attributes.Add("onclick","fooClick('" + li.Value + "','" + tbFoo.ClientID + "');"); }
The important point to realise is that ASP.NET Server controls typically get assigned their ID and Name dynamically at runtime. The last part of this solution solves that by attaching the javascript to the objects on the server side, where it knows the ClientID it has already generated for the control we want to interact with.
- Sent (0)
- New
Generate your business videos by AI with voice or just text
chatGPTaz.com
Talk to ChatGPT by your mother language
AppAIVideo
Your first FREE AI Video App
Deepfake Video
Deepfake AI Video Maker
Deepfake
Deepfake AI Video Maker
AI Deep Fake
Deepfake AI Video Maker
AIvidio
AI Video Mobile Solutions
AIvideos
AI Video Platform & Solutions
AIvedio
AI Video App Maker
Faceswap AI Online
Swap Faces Video, Photo & GIFs Instantly with Powerful AI Tools - Faceswap AI Online FREE
Faceswap AI Online
Swap Faces Video, Photo & GIFs Instantly with Powerful AI Tools - Faceswap AI Online FREE
Temu Free $500 for New Users
Claim Free Temu $500 Credit via Affiliate & Influencer Program
Free TikTok Ads Credit
Master TikTok Ads for Your Business Marketing
Dall-E-OpenAI.com
Generate creative images automatically with AI
chatGPT4.win
Talk to ChatGPT by your mother language
First AI Product from Elon Musk - Grok/UN.com
Speak to Grok AI Chatbot with Your Language
Tooly.win
Open tool hub for free to use by any one for every one with hundreds of tools
GateIO.gomymobi.com
Free Airdrops to Claim, Share Up to $150,000 per Project
iPhoneKer.com
Save up to 630$ when buy new iPhone 16
Buy Tesla Optimus Robot
Order Your Tesla Bot: Optimus Gen 2 Robot Today for less than $20k