简体   繁体   中英

onclick event for radio button using c#

How do I implement “onclick” event using JavaScript for radio button controls in a C# class to change a hidden Server Control Textbox Text value to reflect which button is clicked? The buttons are html controls running on the client.

Say I have 2 radio buttons: Button1 and Button2. In my C# code, I need to replace the alert calls below to update my Textbox control, the value in this hidden will be processed on PostBack.

Button1.Attributes.Add("onclick", "alert(' You clicked Button 1. How do I update textBox.Text? in here?');");       
Button2.Attributes.Add("onclick", "alert('You clicked Button 2. How do I update textBox.Text? in here?');"); 

Try this one

document.getElementById('myTextbox').value = 'Button1';

Because your hidden text box runs at the server side, it's Id is unknown. You need to obtain the client id.

Button1.Attributes.Add("onclick", "javascript:document.getElementById('" + Textbox1.ClientId + "').value = this.ID");

Button2.Attributes.Add("onclick", "javascript:document.getElementById('" + Textbox1.ClientId + "').value = this.ID");

Remember you need to hide the text box with JavaScript or use a hiddenInput control instead.

Button1.Attributes.Add("onclick", "javascript:document.getElementById('" + TextBox1.ClientID + "').value = 'Button1'");

      Button2.Attributes.Add("onclick", "javascript:document.getElementById('" + TextBox1.ClientID + "').value = 'Button2'");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM