Solved

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

Posted on 2004-04-15
5
370 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Learn by Doing. Anytime. Anywhere.

Do you like to learn by doing?
Our labs and exercises give you the chance to do just that: Learn by performing actions on real environments.

Hands-on, scenario-based labs give you experience on real environments provided by us so you don't have to worry about breaking anything.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

696 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