<asp:gridview id= "GridView1" runat= "Server" autogeneratecolumns= "False"
Datakeynames= "Areaid" datasourceid= "SqlDataSource1" >
<Columns>
<asp:boundfield datafield= "Areaid" headertext= "Areaid" readonly= "True"
sortexpression= "Areaid"/>
<asp:boundfield datafield= "AreaName" headertext= "AreaName"
sortexpression= "AreaName"/>
<asp:boundfield datafield= "pid" headertext= "pid" sortexpression= "pid"/>
<asp:TemplateField>
<ItemTemplate>
<asp:checkbox id= "Chk_del" runat= "server"/>//used to determine whether the row to be deleted
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:sqldatasource id= "SqlDataSource1" runat= "Server"
connectionstring= "<%$ connectionstrings:work_oaconnectionstring%>"
Selectcommand= "SELECT * from [area]" ></asp:SqlDataSource>
<asp:checkbox id= "Chk_js" runat= "Server" text= "All Selected (JS)" onclick= "SelectAll (This)"/>//through JS to select all
The JS function is defined as follows
<script type= "Text/javascript" >
Sets the selected state of all the checkbox based on the checked state of the incoming checkbox
function SelectAll (obj)
{
var allinput = document.getelementsbytagname ("input");
alert (allinput.length);
var looptime = allinput.length;
for (i = 0;i < looptime;i++)
{
alert (Allinput[i].type);
if (Allinput[i].type = "checkbox")
{
allinput[i].checked = obj.checked;
}
}
}
</script>
tips for deleting buttons
<asp:button id= "btn_deleterecords" runat= "Server" onclientclick= "return Judgeselect ();" text= "Delete checked records"/>
Add JS Script
Determine if the record is checked and the user confirms the deletion
function Judgeselect ()
{
var result = false;
var allinput = document.getelementsbytagname ("input");
var looptime = allinput.length;
for (i = 0;i < looptime;i++)
{
if (allinput[i].checked)
{
result = true;
Break
}
}
if (!result)
{
Alert ("Select the record you want to delete.") ");
return result;
}
result = Confirm ("Are you sure you want to delete the selected records?") ");
return result;
}
The second way is to implement a full selection of server-side code
<asp:checkbox id= "Chk_server" runat= "Server" oncheckedchanged= "chk_server_checkedchanged" Text= "Select All (Server)" autopostback= "True"/>
The event code is as follows
int a = this. GridView1.Rows.Count;
for (int i = 0; i < A; i++)
{
CheckBox chk = (checkbox) this. Gridview1.rows[i]. FindControl ("Chk_del");
Chk. Checked = this.chk_Server.Checked;
}