asp gridview批量删除和全选
2018-06-23 22:58:37来源:未知 阅读 ()
本人新手刚学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");
}
}
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- JS批量操作CSS属性详细解析 2020-02-29
- Aspnet Mvc 前后端分离项目手记(四)vue项目的搭建(一)(i 2019-08-14
- js setattribute批量设置css样式的方法 2019-04-18
- HTML页面调用ASP参数的方法 2019-04-04
- 详解jscript.encode代码的asp函数 2019-04-04
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash