uncheck radio button

Hi,

I have a radio button like this:

<asp:RadioButton ID="everywhere" runat="server" Text= "everywhere" Checked=false EnableViewState=true />

It can be checked but when I click on it again, it will not uncheck. Is there a simple solution to this?

I guess I could write some client-side code (JavaScript) or some server side code using OnCheckedChanged. However, that seems to be too difficult.

If there is no 'easy solution' could someone please provide some JavaScript to solve my tiny problem. Many thanks.

BW

Chris
csetzkornAsked:
Who is Participating?
 
Ashish PatelCommented:
Okay here we go.
1. In the code of html replace <asp:RadioButton ID="everywhere" runat="server" Text= "everywhere" Checked=false EnableViewState=true /> with  <asp:RadioButton ID="everywhere" runat="server" Text= "everywhere" onMouseOver="FocusRadioObject(this)" onClick="FlipRadioSelection(this)" Checked=false EnableViewState=true />

2. The javascript supporting that is below which you have to add in your HTML code between <head> and </head>
<srcipt>
var objRadChecked;
objRadChecked = false;

function FocusRadioObject(obj) {
      objRadChecked = obj.checked;
}

function FlipRadioSelection(obj) {
      if(objRadChecked == true)
            obj.checked=false;
      else
            obj.checked=true;
      
      objRadChecked = obj.checked;
}
</script>
0
 
Ashish PatelCommented:
if (document.forms[0].everywhere.checked)
 document.forms[0].everywhere.checked = true;
else
document.forms[0].everywhere.checked = false;
0
 
Ashish PatelCommented:
Put that in onClientClick="doCheck()"
and the function
function doCheck() {
if (document.forms[0].everywhere.checked)
 document.forms[0].everywhere.checked = true;
else
document.forms[0].everywhere.checked = false;
}
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
Ashish PatelCommented:
This would be your tag
<asp:RadioButton ID="everywhere" runat="server" Text= "everywhere" Checked=false onClientClick="doCheck()"
EnableViewState=true />
0
 
csetzkornAuthor Commented:
Hi,

Thanks. I tried something similar yesterday. Your code did not work. So I looked at the HTML code. The relevant bit is this:

<div class="frame">
                              <span onClientClick="doCheck()"><input id="ctl00_ContentPlaceHolder1_everywhere" type="radio" name="ctl00$ContentPlaceHolder1$everywhere" value="everywhere" /><label for="ctl00_ContentPlaceHolder1_everywhere">everywhere</label></span>
                        </div>

I adapted the JavaScript code like this:

function doCheck() {
if (document.aspnetForm.ctl00$ContentPlaceHolder1$everywhere.checked)
 document.aspnetForm.ctl00$ContentPlaceHolder1$everywhere.checked = true;
else
document.aspnetForm.ctl00$ContentPlaceHolder1$everywhere.checked = false;
}

Unfortunately it still does not work.

Any ideas? Thanks for your help.

C

PS: It still seems a lot of effort to be able to uncheck a radio button ...
0
 
Ashish PatelCommented:
you onClientClick="doCheck()" in is wrong place. Its in span tag. Let me give a code to add for you at the code behind for firing from radio button.
0
 
csetzkornAuthor Commented:
Hi,

Thanks, I would know how to do the code behind. I prefer to avoid round trips as the form is already quite involved.

Do you reckon I should get rid off the span tag?

C
0
 
Ashish PatelCommented:
Did that work?
0
 
csetzkornAuthor Commented:
Thanks this work like a charm (little error: srcipt -> script).

I will accept your answer but I am still surprised that it is so 'hard' to uncheck a radio button. Should ASP.NET not generate the necessary JavaScript etc.?

Thanks again.

C
0
 
Ashish PatelCommented:
No it doesnt, this is the smallest and smartest stuff which i coded once for myself. This code even works for radio buttons in groups too. Thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.