• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 373
  • Last Modified:

How to validate/group a Checkbox along with a CheckboxList in ASP.NET?

My Web form has two choices for the User, and the choices are mutually exclusive. However, Option 2 (being awake), allows for more than one checkbox. How can I utilize a GroupName to these two controls to enforce that Option 1 is not checked if one of the Option 2 listitems is selected?
Option 1: sleep
Option 2: drive and/or sing and/or eat

<tr>
<td><asp:label id="LabelSleep" Runat="server">Option 1:</asp:label></td>
<td><asp:checkbox id="CheckBoxsleep" runat="server" Text="Sleep"</asp:checkbox></td>
</tr>
<tr>
<td><asp:label id="LabelAwake" Runat="server">Option 2:</asp:label></td>
<td><asp:CheckBoxList id="CheckBoxList1" runat="server"  RepeatDirection="Horizontal">
<asp:ListItem Value="1">Drive</asp:ListItem>
<asp:ListItem Value="1">Sing</asp:ListItem>
<asp:ListItem Value="1">Eat</asp:ListItem>
</asp:CheckBoxList></td>
</tr>
0
bigsplash
Asked:
bigsplash
2 Solutions
 
mmarinovCommented:
you can make Checkboxlist autopostback=true and in the click event check if there is checked checkbox to uncheck the checlbox in the Option 1
B..M
0
 
waterhiddenCommented:
best way is using javascript!


javascript functions in html code:

            <script language=javascript>

function hepsi(sayi)
{
      var i= parseInt(sayi);
      i++;
      i++;
      var isim="";
      var x=2;
            if(document.Form1.DataGrid1__ctl1_CheckBox3.checked==true)  // main checkbox id
            { while(x<=i)
                  { isim="DataGrid1__ctl"+x+"_CheckBox1";  // other checkboxes
                  var is= document.getElementById(isim);
                  is.checked=true;
                  x++;
                  }
                  }else if(document.Form1.DataGrid1__ctl1_CheckBox3.checked==false)
                        { while(x<=i)
                        { isim="DataGrid1__ctl"+x+"_CheckBox1";  //other checkboxes
                        var is= document.getElementById(isim);
                        is.checked=false;
                        x++;
                  }
      }
}
            
      </script>




the checkbox which will check or uncheck other checkboxes:

<asp:CheckBox id=CheckBox3 runat="server" Visible="True" onclick='<%#hepsi%>'></asp:CheckBox>





and in codebehind:

    Public hepsi As String

before CheckBox3 is created:         hepsi = "hepsi(" & e.Item.Cells.Count & ");"




hope it works..
0
 
bigsplashAuthor Commented:
The two posted answers have helped me, however I'm still looking over waterhidden's answer.  The below code has proven to be a sucessful server-side validation solution, however ideally I want to have a client-side custom validator.

'Validate the User's Option choice. Ideally, this should be a client-side custom validator.
                If CheckBoxList1.Items(0).Selected Or CheckBoxList1.Items(1).Selected Or CheckBoxList1.Items(2).Selected Then
                    If CheckBoxValidateOnly.Checked Then
                        Throw New ArgumentException("Both Options are selected. Please select only Option 1 or Option 2 for your Submission.")
                    End If
                Else
                    If Not CheckBoxValidateOnly.Checked Then
                        Throw New ArgumentException("No Option selected. Please select either Option 1 or Option 2 for your Submission.")
                    End If
                End If
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now