1. ใส่ฟังก์ชั่น ในส่วนของ Javascript

<script language=”javascript”>

function confirmDelete(frm)
{
    // loop through all elements
    for (i=0; i<frm.length; i++)
    {
        // Look for our checkboxes only
        if (frm.elements[i].name.indexOf(“DeleteThis”) !=-1)
        {
            // If any are checked then confirm alert, otherwise nothing happens
            if(frm.elements[i].checked)
            {
                return confirm (‘Are you sure you want to delete your selection(s)?’)
                }
        }
    }
}

function select_deselectAll(chkVal, idVal)
{
var frm = document.forms[0];
// Loop through all elements
for (i=0; i<frm.length; i++)
{
// Look for our Header Template’s Checkbox
      if (idVal.indexOf (‘CheckAll’) != -1)
        {
            // Check if main checkbox is checked, then select or deselect datagrid checkboxes
            if(chkVal == true)
            {
                frm.elements[i].checked = true;
            }
            else
            {
                frm.elements[i].checked = false;
            }
            // Work here with the Item Template’s multiple checkboxes
        }
        else if (idVal.indexOf (‘DeleteThis’) != -1)
        {
            // Check if any of the checkboxes are not checked, and then uncheck top select all checkbox
            if(frm.elements[i].checked == false)
            {
                frm.elements[1].checked = false; //Uncheck main select all checkbox
            }
        }
        }
        }
  </script>2. ตั้งชื่อ checkBox ที่ Header Template เป็น CheckAll

3. ตั้งชื่อ checkBox ที่ Item Template เป็น deleteThis

4. หน้า HTML

<HeaderTemplate>
       <asp:CheckBox id=”CheckAll” onclick=”javascript: return select_deselectAll(this.checked, this.id);”
        runat=”server”></asp:CheckBox>
</HeaderTemplate>

5.

<asp:TemplateColumn Visible=”False”>
      <ItemTemplate>
       <asp:Label id=namex runat=”server” text='<%# Container.DataItem(“computerID”) %>’>
       </asp:Label>
      </ItemTemplate>
     </asp:TemplateColumn>

6. ใส่สคริปที่ปุ่ม delete

Private Sub delButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles delButton.Click
        Dim str As String

        For Each i As DataGridItem In DataGrid1.Items
            Dim chkItem As CheckBox = CType(i.FindControl(“DeleteThis”), CheckBox)
            If chkItem.Checked = True Then
                Dim lb As Label = CType(i.FindControl(“namex”), Label)
                delete_grid(lb.Text)
                str = str & lb.Text
            End If
        Next

        Label18.Text = str
        bindg1()
End Sub

7. ระวังเรื่องการกดปุ่ม หลังการกดปุ่ม ต้องไม่ binddatagrid ใหม่อีก เพราะว่าการ check จะหายหมด

if not page.ispostback then

    binddatagrid()

end if

8. กรณีต้องการให้ขึ้นไดอะล็อกเพื่อ confirm ให้ใส่สริปนี้ที่ page_load

Dim buttonx As WebControl = CType(Page.FindControl(“Button2“), WebControl)
        buttonx.Attributes.Add(“onclick”, “return confirmDelete(this.form);”)

button2 คือชื่อ id ของปุ่ม

Private Sub delButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles delButton.Click
        Dim str As String

        For Each i As DataGridItem In DataGrid1.Items
            Dim chkItem As CheckBox = CType(i.FindControl(“DeleteThis”), CheckBox)
            If chkItem.Checked = True Then
                Dim lb As Label = CType(i.FindControl(“namex”), Label)
                delete_grid(lb.Text)
                str = str & lb.Text
            End If
        Next

        Label18.Text = str
        bindg1()
End Sub