?
Solved

uncheck radio button

Posted on 2007-10-03
10
Medium Priority
?
2,693 Views
Last Modified: 2012-09-11
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
0
Comment
Question by:csetzkorn
  • 7
  • 3
10 Comments
 
LVL 23

Expert Comment

by:Ashish Patel
ID: 20004755
if (document.forms[0].everywhere.checked)
 document.forms[0].everywhere.checked = true;
else
document.forms[0].everywhere.checked = false;
0
 
LVL 23

Expert Comment

by:Ashish Patel
ID: 20004761
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
 
LVL 23

Expert Comment

by:Ashish Patel
ID: 20004770
This would be your tag
<asp:RadioButton ID="everywhere" runat="server" Text= "everywhere" Checked=false onClientClick="doCheck()"
EnableViewState=true />
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:csetzkorn
ID: 20004936
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
 
LVL 23

Expert Comment

by:Ashish Patel
ID: 20004985
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
 

Author Comment

by:csetzkorn
ID: 20004999
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
 
LVL 23

Accepted Solution

by:
Ashish Patel earned 2000 total points
ID: 20005028
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
 
LVL 23

Expert Comment

by:Ashish Patel
ID: 20005072
Did that work?
0
 

Author Comment

by:csetzkorn
ID: 20005087
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
 
LVL 23

Expert Comment

by:Ashish Patel
ID: 20005253
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

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question