Check/Unchecl all checkboxes in datalist control

Hi All
I am displaying a datalist in my ASP.Net Web Page, it is displaying for example 10 records from database; each record displaying one check box as list of email messages in hotmail or yahoo. You can select one message or you can select all messages by clicking a check box placed on top namely “Select All”.

My problem is when I add this functionality using ASP.Net my web page post back that page and selects all records in the datalist.

I want same functionality using Javascript. I will pass ID of datalist control and ID of that check box in datalist control. Javascript should select or deselect all records displayed in that datalist. We will need a for loop to iterate through all records displayed in datalist and find the checkbox control and then we will have to change the state, if checked then uncheck otherwise checked.

All this I want just to avoid postback.

I hope you will understand my requirement. Thanks in advance

Check All or None Checkboxes
http://homepage.ntlworld.com/vwphillips/FormCompendium/CheckBoxesCheckAllNone.htm

Thankyou, it is working but I just want to iterate through datalist items. Items in datalist are not fix, these are coming from database tables.

I will try to get an idea from your post. Thanks again

Hello Kravvitz

Actually the code you have sent me that select/deSelect all check box in one group but in my case, I have one datalist, bound to one database table, added one check box namely chk1 for example, ID of check box in all records whether there are 10 records or 100; ID of check box will remain chk1, we just want to run a for loop on datalist to go on each record and set status of chk1 to checked or unchecked. I have done this using the following code in ASP.Net:

    Dim asr As CheckBox
    Dim currStatus As Boolean = IIf(chkAll.Checked, True, False)
    Dim i As Integer = 0

    For i = 0 To dlRegions.Items.Count - 1
        asr = dlRegions.Items(i).FindControl("chk1")
        asr.Checked = currStatus
    Next

In this code ChkAll is the ID of main checkbox which is placed on top of datalist and chk is the check box in each record, dlRegions is the name of datalist. I just want this kind of code which should go through the datalist and uncheck/check all check box.

Regards

Thankyou for your support, finally I have got the solution from internet, which is here:
Write a script in the head as follows:

<script>
function CheckAllDataListCheckBoxes(aspCheckBoxID, checkVal) {
re = new RegExp(‘:’ + aspCheckBoxID + ‘$’)
for(i = 0; i < document.forms[0].elements.length; i++) {
elm = document.forms[0].elements[i]
if (elm.type == ‘checkbox’) {
if (re.test(elm.name)) {
elm.checked = checkVal
}
}
}
}
</script>

and Use the following to add CheckBox in your Page which will select all checkbox from one datalist or datagrid:

<INPUT id=“chkAllItems” onclick=“CheckAllDataListCheckBoxes(‘chk1’,document.forms[0].chkAllItems.checked)” type=“checkbox”>

The 1st parameter with the function is checkbox name which is in the datalist or datagrid and 2nd parameter is status which you want i.e (true or false)