Wrox Programmer Forums
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 16th, 2007, 02:13 AM
Authorized User
 
Join Date: Jun 2007
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

 
Old July 17th, 2007, 01:04 AM
Friend of Wrox
 
Join Date: Aug 2004
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.
 
Old July 17th, 2007, 01:26 AM
Authorized User
 
Join Date: Jun 2007
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

 
Old July 17th, 2007, 04:04 AM
Friend of Wrox
 
Join Date: Aug 2004
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.
 
Old July 17th, 2007, 04:48 AM
Authorized User
 
Join Date: Jun 2007
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

 
Old July 17th, 2007, 06:26 AM
Friend of Wrox
 
Join Date: Aug 2004
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.





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





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