Solved

ASP.Net/VB.Net/DataList: How to reset all the checkboxes in a datalist

Posted on 2004-08-10
4
1,640 Views
Last Modified: 2008-01-09
I have a web form that contains a datalist. Each cell of the datalist contains a checkbox. It looks something like this:

[ ] Select All
-----------------
[ ] Person A
[ ] Person B
[ ] Person C
[ ] Person D

The CheckBox.Text property gets its name from databinding to a Name field in a database. The checkbox value is unbound. What I intend to do is insert records into a table for each checked box.

When I turn on the Select All checkbox at the top I want all of the other check boxes to be turned on, and vice versa. What is the code for doing this?
0
Comment
Question by:rgrimm
  • 2
  • 2
4 Comments
 
LVL 18

Assisted Solution

by:DotNetLover_Baan
DotNetLover_Baan earned 250 total points
ID: 11768751
Hi there...
try this.. (with datagrid)
http://www.dotnetjunkies.com/Tutorial/17F2328F-ECB8-478F-A393-3D0E0F024EC0.dcik

Here is a javascript function to achive, select all..

function SelectAll(CheckBoxControl) {

    if (CheckBoxControl.checked == true) {
       
        var i;
       
        for (i=0; i < document.forms[0].elements.length; i++) {
            if ((document.forms[0].elements[i].type == 'checkbox') && (document.forms[0].elements[i].name.indexOf('myDataGrid') > -1)) {
                document.forms[0].elements[i].checked = true;
            }
        }
    }
   
    else {
       
        var i;
       
        for (i=0; i < document.forms[0].elements.length; i++) {
           
            if ((document.forms[0].elements[i].type == 'checkbox') && (document.forms[0].elements[i].name.indexOf('myDataGrid') > -1)) {
               
                document.forms[0].elements[i].checked = false;
            }
        }
    }
}
==================

<HeaderTemplate>
<input type="CheckBox" name="SelectAllCheckBox" onclick="SelectAll(this)">
</HeaderTemplate>

-Baan
0
 
LVL 4

Accepted Solution

by:
rohanpandya earned 250 total points
ID: 11769771
Hi

  Do following
first set property AutoPostback = true of checkbox associate with selectAll and then write following in code behind

 Private Sub chkSelectall_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkSelectall.CheckedChanged
            Dim i
            If chkSelectall.Checked = True Then
                  For i = 0 To dgTest.Items.Count - 1
                        CType(dgTest.Items(i).FindControl("checkbox1"), CheckBox).Checked = True
                  Next
            End If
            If chkSelectall.Checked = False Then
                  For i = 0 To dgTest.Items.Count - 1
                        CType(dgTest.Items(i).FindControl("checkbox1"), CheckBox).Checked = False
                  Next
            End If
      End Sub
0
 
LVL 18

Expert Comment

by:DotNetLover_Baan
ID: 11771908
Hi Rohan,
your idea is good, but because of  "AutoPostback = true " it will need an extra trip to the server. and I guess it is not at all necessary just to check the CheckBoxes. Javascript will work at the client side and will save you from that extra postback
-Baan
0
 
LVL 4

Expert Comment

by:rohanpandya
ID: 11771987
ya it is also work
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

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…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

810 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