前台代码 <asp:templatefieldheadertext="选择"><itemtemplate></itemtemplate><asp:checkboxid="CheckBox"runat="server"></asp:checkbox></asp:templatefield><asp:buttonid="Button"onclick="Button_Click"runat="server"text="全部选中"></asp:button><asp:buttonid="Button"onclick="Button_Click"runat="server"text="删除所选"></asp:button>
后台代码 protectedvoidButton_Click(objectsenderEventArgse){if(ButtonText=="全部选中"){foreach(GridViewRowrowinGridViewRows){CheckBoxCheckBox=(CheckBox)rowCells[]FindControl("CheckBox");CheckBoxChecked=true;}ButtonText="全部不选";}else{foreach(GridViewRowrowinGridViewRows){CheckBoxCheckBox=(CheckBox)rowCells[]FindControl("CheckBox");CheckBoxChecked=false;}ButtonText="全部选中";}}protectedvoidButton_Click(objectsenderEventArgse){foreach(GridViewRowrowinGridViewRows){CheckBoxCheckBox=(CheckBox)rowCells[]FindControl("CheckBox");if(CheckBoxChecked==true){SqlConnectionconn=FunInitConn();SqlCommandcomm=newSqlCommand();commConnection=conn;commCommandText="deletefromCommentwhereComment_ID=’"+rowCells[]Text+"’";connOpen();commExecuteNonQuery();connClose();}}} |