Solved

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

Posted on 2004-04-15
5
364 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

816 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

8 Experts available now in Live!

Get 1:1 Help Now