Solved

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

Posted on 2004-04-15
5
359 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video discusses moving either the default database or any database to a new volume.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

772 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

12 Experts available now in Live!

Get 1:1 Help Now