Solved

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

Posted on 2004-04-15
5
362 Views
Last Modified: 2007-12-19
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
Comment
Question by:bigsplash
5 Comments
 
LVL 28

Accepted Solution

by:
mmarinov earned 125 total points
ID: 10837683
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
 
LVL 2

Assisted Solution

by:waterhidden
waterhidden earned 125 total points
ID: 10839957
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
 

Author Comment

by:bigsplash
ID: 10844306
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

867 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now