标签:
效果图:
后台代码:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class Default2 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } 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 Button1_Click1(object sender, EventArgs e) { CheckBox2.Checked = false; for (int i = 0; i <= GridView1.Rows.Count - 1; i++) { CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1"); cbox.Checked = false; } } protected void Button2_Click(object sender, EventArgs e) { for (int i = 0; i <= GridView1.Rows.Count - 1; i++) { CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1"); if (cbox.Checked == true) { string sqlstr = "delete from ttt where gwbm=‘" + GridView1.DataKeys[i].Value + "‘"; DataAccess myda = new DataAccess(); myda.exeNonQuery(sqlstr); GridView1.DataBind(); } } } }
前台主要代码:
<body> <form id="form1" runat="server"> <div> <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="3" DataKeyNames="gwbm" DataSourceID="SqlDataSource1" EmptyDataText="没有可显示的数据记录。" Font-Size="9pt" Width="1062px"> <Columns> <asp:TemplateField> <ItemTemplate> <asp:CheckBox ID="CheckBox1" runat="server" /> </ItemTemplate> </asp:TemplateField> <asp:BoundField DataField="gwbm" HeaderText="gwbm" ReadOnly="True" SortExpression="gwbm" /> <asp:BoundField DataField="gwmc" HeaderText="gwmc" SortExpression="gwmc" /> <asp:BoundField DataField="dwmc" HeaderText="dwmc" SortExpression="dwmc" /> <asp:BoundField DataField="gwlb" HeaderText="gwlb" SortExpression="gwlb" /> <asp:BoundField DataField="nlyq1" HeaderText="nlyq1" SortExpression="nlyq1" /> <asp:BoundField DataField="nlyq2" HeaderText="nlyq2" SortExpression="nlyq2" /> <asp:BoundField DataField="xlyq" HeaderText="xlyq" SortExpression="xlyq" /> <asp:BoundField DataField="zcyq" HeaderText="zcyq" SortExpression="zcyq" /> <asp:BoundField DataField="jnyq" HeaderText="jnyq" SortExpression="jnyq" /> <asp:BoundField DataField="zwyq" HeaderText="zwyq" SortExpression="zwyq" /> <asp:BoundField DataField="zprs" HeaderText="zprs" SortExpression="zprs" /> <asp:BoundField DataField="gwxc" HeaderText="gwxc" SortExpression="gwxc" /> <asp:BoundField DataField="gzbm" HeaderText="gzbm" SortExpression="gzbm" /> </Columns> <FooterStyle BackColor="White" ForeColor="#000066" /> <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" /> <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" /> <RowStyle ForeColor="#000066" /> <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" /> <SortedAscendingCellStyle BackColor="#F1F1F1" /> <SortedAscendingHeaderStyle BackColor="#007DBB" /> <SortedDescendingCellStyle BackColor="#CAC9C9" /> <SortedDescendingHeaderStyle BackColor="#00547E" /> </asp:GridView> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ygzpConnectionString1 %>" DeleteCommand="DELETE FROM [ttt] WHERE [gwbm] = @gwbm" InsertCommand="INSERT INTO [ttt] ([gwbm], [gwmc], [dwmc], [gwlb], [nlyq1], [nlyq2], [xlyq], [zcyq], [jnyq], [zwyq], [zprs], [gwxc], [gzbm], [gzdd], [gwzz], [gwms]) VALUES (@gwbm, @gwmc, @dwmc, @gwlb, @nlyq1, @nlyq2, @xlyq, @zcyq, @jnyq, @zwyq, @zprs, @gwxc, @gzbm, @gzdd, @gwzz, @gwms)" ProviderName="<%$ ConnectionStrings:ygzpConnectionString1.ProviderName %>" SelectCommand="SELECT [gwbm], [gwmc], [dwmc], [gwlb], [nlyq1], [nlyq2], [xlyq], [zcyq], [jnyq], [zwyq], [zprs], [gwxc], [gzbm], [gzdd], [gwzz], [gwms] FROM [ttt]" UpdateCommand="UPDATE [ttt] SET [gwmc] = @gwmc, [dwmc] = @dwmc, [gwlb] = @gwlb, [nlyq1] = @nlyq1, [nlyq2] = @nlyq2, [xlyq] = @xlyq, [zcyq] = @zcyq, [jnyq] = @jnyq, [zwyq] = @zwyq, [zprs] = @zprs, [gwxc] = @gwxc, [gzbm] = @gzbm, [gzdd] = @gzdd, [gwzz] = @gwzz, [gwms] = @gwms WHERE [gwbm] = @gwbm"> <DeleteParameters> <asp:Parameter Name="gwbm" Type="String" /> </DeleteParameters> <InsertParameters> <asp:Parameter Name="gwbm" Type="String" /> <asp:Parameter Name="gwmc" Type="String" /> <asp:Parameter Name="dwmc" Type="String" /> <asp:Parameter Name="gwlb" Type="String" /> <asp:Parameter Name="nlyq1" Type="Int32" /> <asp:Parameter Name="nlyq2" Type="Int32" /> <asp:Parameter Name="xlyq" Type="Int32" /> <asp:Parameter Name="zcyq" Type="Int32" /> <asp:Parameter Name="jnyq" Type="Int32" /> <asp:Parameter Name="zwyq" Type="Int32" /> <asp:Parameter Name="zprs" Type="Int32" /> <asp:Parameter Name="gwxc" Type="String" /> <asp:Parameter Name="gzbm" Type="String" /> <asp:Parameter Name="gzdd" Type="String" /> <asp:Parameter Name="gwzz" Type="String" /> <asp:Parameter Name="gwms" Type="String" /> </InsertParameters> <UpdateParameters> <asp:Parameter Name="gwmc" Type="String" /> <asp:Parameter Name="dwmc" Type="String" /> <asp:Parameter Name="gwlb" Type="String" /> <asp:Parameter Name="nlyq1" Type="Int32" /> <asp:Parameter Name="nlyq2" Type="Int32" /> <asp:Parameter Name="xlyq" Type="Int32" /> <asp:Parameter Name="zcyq" Type="Int32" /> <asp:Parameter Name="jnyq" Type="Int32" /> <asp:Parameter Name="zwyq" Type="Int32" /> <asp:Parameter Name="zprs" Type="Int32" /> <asp:Parameter Name="gwxc" Type="String" /> <asp:Parameter Name="gzbm" Type="String" /> <asp:Parameter Name="gzdd" Type="String" /> <asp:Parameter Name="gwzz" Type="String" /> <asp:Parameter Name="gwms" Type="String" /> <asp:Parameter Name="gwbm" Type="String" /> </UpdateParameters> </asp:SqlDataSource> <asp:CheckBox ID="CheckBox2" runat="server" AutoPostBack="True" Font-Size="9pt" oncheckedchanged="CheckBox2_CheckedChanged" Text="全选" /> <asp:Button ID="Button1" runat="server" Font-Size="9pt" onclick="Button1_Click1" Text="取消" /> <asp:Button ID="Button2" runat="server" Font-Size="9pt" onclick="Button2_Click" Text="删除" /> </div> </form> </body>
标签:
原文地址:http://www.cnblogs.com/xusy/p/4891106.html