GridView+CheckBox删除记录<ItemTemplate>。。。</ItemTemplate> 前台 <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False" onpageindexchanging="GridView1_PageIndexChanging" onrowdeleting="GridView1_RowDeleting" style="margin-right: 0px" ShowHeader="False" BorderWidth="0" cellspacing="0" CellPadding="0" Width="100%" > <PagerSettings FirstPageText="第一页" LastPageText="最后一页" Mode="NextPreviousFirstLast" NextPageText="下一页" PreviousPageText="上一页" /> <Columns> <asp:TemplateField HeaderText="修改"> <ItemTemplate> <table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td height="20" width="10%" class="but" align="center"><%# DataBinder.Eval(Container, "DataItem.id")%></td> <td height="20" width="30%" class="but" align="center"><%# DataBinder.Eval(Container, "DataItem.type")%></td> <td height="20" width="30%" class="but" align="center"><a href='managedept.aspx?id=<%#Eval("id") %>'>修改信息</a> </td> <td height="20" width="30%" class="but" align="center"><asp:CheckBox ID="CheckBox1" runat="server"/></td> </tr> </table> </ItemTemplate> </asp:TemplateField> </Columns> <SelectedRowStyle Font-Bold="False" /> <PagerStyle HorizontalAlign="Left" /> <HeaderStyle Font-Bold="False" /> </asp:GridView> <table align="center" cellpadding="0" cellspacing="0" border="0" width="100%"> <tr> <td colspan="16" height="30" class="but"> <asp:CheckBox ID="CheckBox2" runat="server" AutoPostBack="True" OnCheckedChanged="CheckBox2_CheckedChanged" />选择/反选 <asp:Button ID="Button3" runat="server" Text="删除" OnClick="Button3_Click" /></td> </tr> </table>
后台 protected void CheckBox2_CheckedChanged(object sender, EventArgs e) { for (int i = 0; i <= GridView1.Rows.Count - 1; i++) { CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1"); if (CheckBox2.Checked == true) { cbox.Checked = true; } else { cbox.Checked = false; } } } protected void Button3_Click(object sender, EventArgs e) { Conn conn = new Conn(); SqlConnection con = conn.getConn(); SqlCommand sqlcom; for (int i = 0; i <= GridView1.Rows.Count - 1; i++) { int id = Convert.ToInt32(this.GridView1.DataKeys[i].Value); CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1"); if (cbox.Checked == true) { // string sqlstr = "delete from dept_type where id='" + GridView1.Rows[i].Cells[0].Text + "'"; string sqlstr = "delete from dept_type where id='" + id + "'"; sqlcom = new SqlCommand(sqlstr, con); sqlcom.ExecuteNonQuery(); } } con.Close(); BindData(); }
|