时间:2016-02-16 01:51 来源: 我爱IT技术网 作者:佚名
欢迎您访问我爱IT技术网,今天小编为你分享的编程技术是:【asp.net gridview实现全选,反选与删除记录的操作代码】,下面是详细的讲解!
asp.net gridview实现全选,反选与删除记录的操作代码
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SetDataBinder();
}
Button2.Attributes.Add("onclick","return confirm('你确定要删除所选择的记录么?')");
}
protected void SetDataBinder()
{
string sql="Select * from SendMail";
SqlConnection conn=new SqlConnection(ConfigurationManager.ConnectionStrings["StudyConnectionString"].ToString());
conn.Open();
SqlDataAdapter da=new SqlDataAdapter(sql ,conn );
DataSet ds=new DataSet();
da.Fill(ds,"table");
GridView1 .DataSource=ds.Tables ["table"];
GridView1.DataBind();
conn.Close();
}
/// <summary>
/// 全选记录
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Button1_Click(object sender, EventArgs e)
{
CheckBox cb;
for (int i=0; i < GridView1.Rows.Count; i++)
{
cb=(CheckBox)GridView1.Rows[i].Cells[0].FindControl("CheckBox1");
cb.Checked=true;
}
}
/// <summary>
/// 执行删除操作,删除所选择的项
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Button2_Click(object sender, EventArgs e)
{
string sql="(";
for (int i=0; i < GridView1.Rows.Count; i++)
{
CheckBox cb=(CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
if (cb.Checked==true)
{
sql=sql + Convert.ToInt32(GridView1.DataKeys[i].Value) + ",";
}
}
//去掉最后的逗号,并且加上右手号
sql=sql.Substring(0,sql.Length -1)+")";
sql="delete SendMail where MailID in"+sql;
SqlConnection conn=new SqlConnection(ConfigurationManager.ConnectionStrings["StudyConnectionString"].ToString());
conn.Open();
try
{
//执行删除语句
SqlCommand cmd=new SqlCommand(sql, conn);
int delcount=Convert.ToInt32(cmd.ExecuteNonQuery());
Response.Write("<script>alert('共删除" + delcount + "条数据');</script>");
SetDataBinder();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
finally
{
conn.Close();
}
}
/// <summary>
/// 反选操作
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Button3_Click(object sender, EventArgs e)
{
CheckBox cb;
for (int i=0; i < GridView1.Rows.Count; i++)
{
cb=(CheckBox)GridView1.Rows[i].Cells[0].FindControl("CheckBox1");
if (cb.Checked)
{
cb.Checked=false ;
}
else
{
cb.Checked=true ;
}
}
}
关于asp.net gridview实现全选,反选与删除记录的操作代码的用户互动如下:
相关问题:
答: >>详细
相关问题:
答: >>详细
相关问题:
答: >>详细
- 【asp】asp.net url重写浅谈-net-url重写
- 【DataSet】DataSet、DataTable、DataRow区别详解
- 【asp】asp.net 动态添加多个用户控件-net-动态添
- 【ASP】ASP.NET中内嵌页面代码的一个问题-NET-内
- 【As】Asp.net中的页面乱码的问题-sp--pn-ne-et
- 【增加记录】asp.net中获取新增加记录的ID Access
- 【创建】ASP.NET Web API教程 创建域模型的方法详
- 【Asp】Asp.net 页面调用javascript变量的值-net-
- 【ASP】ASP.NET 5升级后如何删除旧版本的DNX-NET5
- 【404页面】ASP.NET设置404页面返回302HTTP状态码
- 评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)
-
