本人新手刚学asp.net   全选和删除也是引用了他人的代码试了一试可以实现,感觉很好,就发了上来。

前台代码

<asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False"
CellPadding="3" Font-Size="9pt" BackColor="White" BorderColor="#CCCCCC"
BorderStyle="None" BorderWidth="1px" Width="675px"
onrowdatabound="GridView1_RowDataBound" DataKeyNames="项目id"
>
<FooterStyle BackColor="White" ForeColor="#000066" />
<Columns>
<asp:TemplateField HeaderText="选择">
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="项目名称" HeaderText="项目名称" />
<asp:BoundField DataField="项目状态" HeaderText="项目状态" />
<asp:TemplateField HeaderText="详细">
<ItemTemplate>
<a href="Detailed.aspx?id='<%#Eval ("项目id") %>'">
<asp:Label ID="lbl详细" runat="server" Text="详细"></asp:Label></a>
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="编辑">
<ItemTemplate>
<a href="EditProject.aspx?id='<%#Eval ("项目id") %>'">
<asp:Label ID="lbl编辑" runat="server" Text="编辑"></asp:Label></a>
</ItemTemplate>
</asp:TemplateField>

</Columns>
<RowStyle ForeColor="#000066" />
<SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
</asp:GridView>

后台代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{

protected void Page_Load(object sender, EventArgs e)
{

if (!IsPostBack)
{
bind();
}
}

public void bind()
{
SqlConnection con = new SqlConnection("server=.;database=投票调查数据库;uid=sa;pwd=401");
string sql = "select * from 项目表";
SqlCommand com = new SqlCommand(sql, con);
SqlDataAdapter da = new SqlDataAdapter(com);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
//全选
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_Click(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)
{

string sql = "delete from 项目表 where";
string cal = " ";
for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox cbox = (CheckBox)GridView1.Rows[i].Cells[0].FindControl("CheckBox1");
if (cbox.Checked == true)
{
cal += " 项目id=" + GridView1.DataKeys[i].Value.ToString() + " or";
}
}
if (cal != "")
{
sql += cal.Substring(0, cal.Length - 3);
}
else
{ sql = ""; }
//Response.Write(sql);//这里可以自己定义程序,进行删除任务
SqlConnection con = new SqlConnection("server=.;database=投票调查数据库;uid=sa;pwd=401");
con.Open();
SqlCommand com = new SqlCommand(sql, con);
com.ExecuteNonQuery();
bind();

}
//查询
protected void Button1_Click1(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("server=.;database=投票调查数据库;uid=sa;pwd=401");
string name = this.TextBox1.Text;

string sql = "select * from 项目表 where 项目id='" + TextBox1.Text + "'";

SqlCommand cmd = new SqlCommand(sql, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onmouseover", "if(this!=prevselitem){this.style.backgroundColor='#Efefef'}");//当鼠标停留时更改背景色
e.Row.Attributes.Add("onmouseout", "if(this!=prevselitem){this.style.backgroundColor='#ffffff'}");//当鼠标移开时还原背景色
e.Row.Attributes.Add("onclick", e.Row.ClientID.ToString() + ".checked=true;selectx(this)");//点击行变色

e.Row.Attributes["style"] = "Cursor:hand"; //设置悬浮鼠标指针形状为"小手"

}
}
protected void Button4_Click(object sender, EventArgs e)
{
Response.Redirect("add.aspx");
}
}

asp gridview批量删除和全选的更多相关文章

  1. vue.js 批量删除跟全选,反选效果

    <!DOCTYPE html><html lang="en"> <head> <meta charset="UTF-8" ...

  2. Android ListView批量选择(全选、反选、全不选)

    APP的开发中,会常遇到这样的需求:批量取消(删除)List中的数据.这就要求ListVIew支持批量选择.全选.单选等等功能,做一个比较强大的ListView批量选择功能是很有必要的,那如何做呢? ...

  3. Java Swing 如何实现记事本中“编辑”菜单下的 剪切,复制,粘贴,删除,全选 功能

    这篇文字将要学习以下知识点: 1.如何给JButton按钮添加鼠标点击事件监听器 #1.addMouseListener(MouseListener l)  给JButton添加一个鼠标点击监听器l ...

  4. 【HTML5】页面点击按钮添加一行 删除一行 全选 反选 全不选

    页面点击按钮添加一行    删除一行   全选   反选  全不选 页面效果图如下 html页面代码 <!DOCTYPE html> <html> <head> & ...

  5. 安卓ListView中CheckBox的使用(支持Item列表项的删除,全选,全不选)

    ListView 自身提供了 CheckBox 只需要添加一行代码 getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); 但是这种实现 ...

  6. Asp.net中文本框全选的实现

    一.鼠标滑过textbox全选 前台: <asp:TextBox runat="server" onMouseOver="this.focus();this.sel ...

  7. 1如何给devexpress的gridview控件绘制全选按钮

    1 首先注册gridview的this.edibandedGridView.CustomDrawColumnHeader += EdibandedGridView_CustomDrawColumnHe ...

  8. asp.net 批量删除

    直接上代码: 1.页面部分 <script type="text/javascript" src="http://code.jquery.com/jquery-1. ...

  9. js gridview中checkbox的全选与全不选

    1.html: <asp:GridView runat="server" ID="gvAddBySR" AutoGenerateColumns=" ...

随机推荐

  1. VS2012中使用纯C实现COM的小问题

    用VS2012新建C++工程都预定义了宏__cplusplus,所以引用到的都是C++的定义.但是要用C实现的话,一般都是也就不是C++的了.比如以下代码: #undef INTERFACE #def ...

  2. ChannelFactory.Endpoint 上的地址属性为空。ChannelFactory 的终结点必须指定一个有效的地址。

    主体代码如下 IServiceA proxyA; ChannelFactory<IServiceA> factoryA = new ChannelFactory<IServiceA& ...

  3. 两款.net 下编辑器小结

    在asp.net 状态下,有两款不错的网页编辑器,分别是freetextbox和fckeditor,网上不少文章介绍之,现归纳之 1 freetextox      安装时引用freetexbox.d ...

  4. android开发板

    element14-beaglebone-black http://www.embest-tech.cn/shop/star/element14-beaglebone-black-rev-c.html ...

  5. 段错误调试神器 - Core Dump详解

    一.前言: 有的程序可以通过编译, 但在运行时会出现Segment fault(段错误). 这通常都是指针错误引起的. 但这不像编译错误一样会提示到文件某一行, 而是没有任何信息, 使得我们的调试变得 ...

  6. VS发布,TFS邮件设置截图

    vs2012发布截图: TFS邮件设置截图:

  7. JavaScript高级程序设计44.pdf

    unload事件 与load事件对应的是unload事件,这个事件在文档被完全卸载后触发,只要用户从一个页面切换到另一个页面,就会发生unload事件,最多的情况是清除引用,避免内存泄漏 与load事 ...

  8. NIR相机

    近红外(NIR)相机——专为低照度环境而设计的高灵敏度相机 近红外光”(Near-Infrared) 是介于可见光和中红外光间的电磁波,因此是不能被人眼所察觉到的.近红外优化工业相机广泛适用于交通监控 ...

  9. 超强vim配置文件

    简易安装方法: https://github.com/ma6174/vim 打开终端,执行下面的命令就自动安装好了: wget -qO- https://raw.github.com/ma6174/v ...

  10. myeclipse开发代码颜色搭配保护视力

    废话不多说,这个东西主要是为了保护视力的,另外我也挺喜欢上边的颜色搭配的,今天特拿出来分享.直接上图