Wrox Programmer Forums

Need to download code?

View our list of code downloads.

| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
ASP.NET 1.0 and 1.1 Professional For advanced ASP.NET 1.x coders. Beginning-level questions will be redirected to other forums. NOT for "classic" ASP 3 or the newer ASP.NET 2.0 and 3.5
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Professional section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old July 16th, 2007, 02:13 AM
Authorized User
 
Join Date: Jun 2007
Location: , , .
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Default Loop over check boxes

Hi
Please help me out with this problem.
There are many check boxes created at runtime on a webform . I want to check that at least one check box must be checked. How should I go about it using C#.

-- Sams

  #2 (permalink)  
Old July 17th, 2007, 01:04 AM
Friend of Wrox
Points: 1,935, Level: 17
Points: 1,935, Level: 17 Points: 1,935, Level: 17 Points: 1,935, Level: 17
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2004
Location: United Kingdom
Posts: 550
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hi

To help you further, please let us know exactly how the checkboxes are being created? Let us know what code you write to create these runtime checkboxes...

Regards
Mike

Don't expect too much, too soon.
  #3 (permalink)  
Old July 17th, 2007, 01:26 AM
Authorized User
 
Join Date: Jun 2007
Location: , , .
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi

The code to create the check boxes is roughly given below:

        HtmlInputCheckBox objChk=new HtmlInputCheckBox();
    objChk.ID="chkBox"+i.ToString();

Thanks

  #4 (permalink)  
Old July 17th, 2007, 04:04 AM
Friend of Wrox
Points: 1,935, Level: 17
Points: 1,935, Level: 17 Points: 1,935, Level: 17 Points: 1,935, Level: 17
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2004
Location: United Kingdom
Posts: 550
Thanks: 0
Thanked 1 Time in 1 Post
Default

You can simply call a javascript function on a button's click event as below:

<input type="button" value="Button" onclick="return Validate()">

*****Javascript function to validate*********
function Validate()
        {
            var iElmCount, counter = 0;

            iElmCount = document.forms[0].elements.length;
            var obj = document.forms[0].elements;

            for (var i = 0; i <= iElmCount - 1; i ++)
            {
                if(obj[i].type == "checkbox")
                {
                    if (obj[i].checked)
                    {
                        counter += 1;
                    }
                }
            }

            if (counter == 0)
            {
                alert("Please select a checkbox.");
                return false;
            }

            return true;
        }

Regards
Mike

Don't expect too much, too soon.
  #5 (permalink)  
Old July 17th, 2007, 04:48 AM
Authorized User
 
Join Date: Jun 2007
Location: , , .
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks a lot for your help. Your reply has solved my problem.

Thanks
Sams

  #6 (permalink)  
Old July 17th, 2007, 06:26 AM
Friend of Wrox
Points: 1,935, Level: 17
Points: 1,935, Level: 17 Points: 1,935, Level: 17 Points: 1,935, Level: 17
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2004
Location: United Kingdom
Posts: 550
Thanks: 0
Thanked 1 Time in 1 Post
Default

Glad I could be of help :)

Regards
Mike

Don't expect too much, too soon.
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Check Boxes Allan320 Access VBA 3 June 2nd, 2006 05:45 AM
Loop through combo boxes Scootterp Access VBA 5 February 24th, 2006 12:06 PM
Disable Check Boxes sweet4511 VB How-To 22 July 19th, 2005 03:53 PM
Using check boxes in datagrid ractim ADO.NET 2 September 8th, 2004 08:28 AM
Getting value of Dynamic Check boxes kathryne Classic ASP Databases 3 March 19th, 2004 05:44 AM



All times are GMT -4. The time now is 07:46 AM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.