aspx
<script type="text/javascript"> function CheckAll(Obj) { var AllObj = document.all; if (Obj.checked)//全选 { for (var i = 0; i < AllObj.length; i++) { if (AllObj[i].type == "checkbox") { AllObj[i].checked = true; } } } else//反选 { for (var i = 0; i < AllObj.length; i++) { if (AllObj[i].type == "checkbox") { AllObj[i].checked = false; } } } } </script> <body> <form id="form2" runat="server"> <div> <asp:DataList ID="DataList1" runat="server" Width="465px" onitemcommand="DataList1_ItemCommand" DataKeyField="id"> <HeaderTemplate> <asp:CheckBox ID="CheckBox2" runat="server" οnclick="return CheckAll(this)" /> <asp:Label ID="Label3" Width="50px" runat="server" Text="ID"></asp:Label> <asp:Label ID="Label4" Width="50px" runat="server" Text="姓名"></asp:Label> <asp:Label ID="Label5" Width="50px" runat="server" Text="性别"></asp:Label> <asp:Label ID="Label7" Width="100px" runat="server" Text="电话"></asp:Label> <asp:Label ID="Label6" Width="50px" runat="server" Text="操作:"></asp:Label> </HeaderTemplate> <ItemTemplate> <asp:CheckBox ID="CheckBox1" runat="server" /> <asp:Label ID="Label1" Width="50px" runat="server" Text='<%# Eval( "id") %>'></asp:Label> <asp:Label ID="Label2" Width="50px" runat="server" Text='<%# Eval( "StuName") %>'></asp:Label> <asp:DropDownList ID="ddlSex" Width="50px" runat="server"> <asp:ListItem Value="0" > 男 </asp:ListItem> <asp:ListItem Value="1"> 女 </asp:ListItem> </asp:DropDownList> <asp:Label ID="Label8" Width="100px" runat="server" Text='<%# Eval( "StuPhone") %>'></asp:Label> <asp:Button ID="Button1" Width="50px" runat="server" CommandName="singleDelete" Text="delete" /> </ItemTemplate> <FooterTemplate> <asp:Button ID="Button2" runat="server" CommandName="mutlDelete" Text="deleteAll" /> </FooterTemplate> </asp:DataList> </div> </form> </body>
aspc.cs
public partial class _238DllInGridView : System.Web.UI.Page { ShowImageBll showBll = new ShowImageBll(); protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { BindDataList(); sexBind(); } } public void BindDataList() { DataSet ds = showBll.GetStudentList(); DataList1.DataSource = ds; DataList1.DataBind(); } public void sexBind() { DataSet ds = showBll.GetStudentList(); DropDownList ddlSex; for (int i = 0; i < DataList1.Items.Count;i++ ) { if (ds.Tables[0].Rows[i]["StuSex"].ToString() == "男")//数据库性别没有设置,ddlSex控件将默认设置为“男” { ddlSex = (DropDownList)DataList1.Items[i].FindControl("ddlSex"); ddlSex.SelectedValue = "0"; } if (ds.Tables[0].Rows[i]["StuSex"].ToString() == "女") { ddlSex = (DropDownList)DataList1.Items[i].FindControl("ddlSex"); ddlSex.SelectedValue="1"; } } } protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e) { switch (e.CommandName) { case "singleDelete": int id = int.Parse(DataList1.DataKeys[e.Item.ItemIndex].ToString()); showBll.DeleteStudent(id); Response.Write("<script>alert('删除成功!')</script>"); BindDataList(); break; case "mutlDelete": DataListItemCollection dlic = DataList1.Items;//创建一个DataList列表项集合对象 for (int i = 0; i < dlic.Count; i++) { CheckBox cb=(CheckBox)dlic[i].FindControl("CheckBox1"); if (cb.Checked) { int id1 = int.Parse(DataList1.DataKeys[dlic[i].ItemIndex].ToString()); showBll.DeleteStudent(id1); } } BindDataList();//重新绑定数据库 break; } } }