4 Replies - 15413 Views - Last Post: 01 February 2011 - 05:28 AM Rate Topic: -----

#1 MarmiteX1  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 159
  • Joined: 17-October 09

Allowing only one checkbox selection

Posted 27 January 2011 - 12:49 PM

Hi,

I have a ASP.net web form which has two checkboxes Accept and Decline. I am using ASP.NET with C#. I am using ASP.net ContentPlace holders.

I would like to allow my end user to be able to select one of the two checkboxes. I thought I would try and use Javascript but I am new to it.

How do I go about wiring up the Javascript function to the checkboxes for it to work based on the below?

<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
<div class="row">
      <div class="label">Accept: </div>
          <div class="field">
            <asp:CheckBox ID="acceptCheckBox" runat="server"  />
       </div>
</div>

 

<div class="row">
        <div class="label">Reject: </div>
              <div class="field">
                     <asp:CheckBox ID="DeclineCheckbox"  runat="server" />
              </div>
  </div>
</asp:ContentPlaceHolder>



Below is the javascript I am using:

<script language="javascript">
function getObj(name)
{
/* DHTML Micro API
* Source: http://www.quirksmode.org/js/dhtmloptions.html
*/
if (document.getElementById) // test if browser supports document.getElementById
{
this.obj = document.getElementById(name);
this.style = document.getElementById(name).style;
}
else if (document.all) // test if browser supports document.all
{
this.obj = document.all[name];
this.style = document.all[name].style;
}
else if (document.layers) // test if browser supports document.layers
{
this.obj = document.layers[name];
this.style = document.layers[name].style;
}
}

function checkTheCheckBoxes()
{
var checkboxOne = new getObj('acceptCheckBox');
var checkboxTwo = new getObj('declineCheckBox');

if (checkboxOne.obj.checked == true)
{
checkboxTwo.obj.disabled = false;
}
else
{
checkboxTwo.obj.disabled = true;
checkboxTwo.obj.checked = false;
}
}
</script>



Do i have to use the OnChecked propery and assign the function to that?

I would greatly appreciate your thoughts on the above.

Thanks in advance

Is This A Good Question/Topic? 0
  • +

Replies To: Allowing only one checkbox selection

#2 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1526
  • View blog
  • Posts: 5,961
  • Joined: 21-March 08

Re: Allowing only one checkbox selection

Posted 27 January 2011 - 01:09 PM

Why not use RadioButtons instead? Their purpose to have only one selection in the group.

If you don't want to change them to RadioButtons, you could try using AJAX's MutuallyExclusiveCheckBox Control.

I think you are going about it wrong trying to manually use javascript to only have one selection.
Was This Post Helpful? 0
  • +
  • -

#3 MarmiteX1  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 159
  • Joined: 17-October 09

Re: Allowing only one checkbox selection

Posted 27 January 2011 - 03:00 PM

View Posteclipsed4utoo, on 27 January 2011 - 01:09 PM, said:

Why not use RadioButtons instead? Their purpose to have only one selection in the group.

If you don't want to change them to RadioButtons, you could try using AJAX's MutuallyExclusiveCheckBox Control.

I think you are going about it wrong trying to manually use javascript to only have one selection.


Thanks for the advice, I may just go ahead and use RadioButtons but I will also look into MutuallyExclusibeCheckBox. Can you provide me an example if possible please?
Was This Post Helpful? 0
  • +
  • -

#4 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1526
  • View blog
  • Posts: 5,961
  • Joined: 21-March 08

Re: Allowing only one checkbox selection

Posted 28 January 2011 - 07:07 AM

Here is a simple example of the code to implement it...

http://www.mindstick...%20in%20ASP.Net
Was This Post Helpful? 0
  • +
  • -

#5 MarmiteX1  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 159
  • Joined: 17-October 09

Re: Allowing only one checkbox selection

Posted 01 February 2011 - 05:28 AM

View Posteclipsed4utoo, on 28 January 2011 - 07:07 AM, said:

Here is a simple example of the code to implement it...

http://www.mindstick...%20in%20ASP.Net


Thanks, much appreciated
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1