gridview-----添加OnRowDeleting事件及确认事件OnRowCreated
来源:互联网 发布:mac应用软件 编辑:程序博客网 时间:2024/06/06 03:38
要注意 :事件的参数类型如GridViewDeleteEventArgs 、GridViewRowEventArgs;把数据库链接和表名做适当的处理.
前台:OnRowDeleting ="GridView1_OnRowDeleting" OnRowCreated="GridView1_RowCreated"
后台:
protected void GridView1_OnRowDeleting(object sender, GridViewDeleteEventArgs e)
{
int id = Convert.ToInt32 ( GridView1.DataKeys[e.RowIndex].Value);//获得要删除行的id
//数据库操作,最好用存储过程
string DeleteCommand;
DeleteCommand = "delete 表名 where SDBP_MID='" +id +"'";
GridViewBind(DeleteCommand , "表名");
//重新绑定gridview
string sqlstr = "select * from 表名";
GridViewBind(sqlstr, "表名");
}
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)//添加确认事件,此在绑定datasource时也可用
{
if (e.Row != null && e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton lb = e.Row.Cells[5].Controls[0] as LinkButton;//Cell[]中应该是你的删除列的列数-1
//如果是Button按钮,即 ButtonType="Button" 时
//则 Button lb = e.Row.Cells[5].Controls[0] as Button;
lb.Attributes.Add("onclick", "return window.confirm('确认要删除该记录吗?')");
}
}
protected void GridViewBind(string sqlstr, string tablename)//根据tablename和sql语句绑定数据库
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings
["tahx2006ConnectionString"].ConnectionString);
SqlDataAdapter da = new SqlDataAdapter(sqlstr, con);
DataSet ds = new DataSet();
da.Fill(ds, tablename );
GridView1.DataSource = ds.Tables[tablename ].DefaultView;
GridView1.DataBind();
}
- gridview-----添加OnRowDeleting事件及确认事件OnRowCreated
- 为GridView添加CheckBox及事件
- OnRowDeleting事件和OnRowCommand事件之间的触发关系
- Gridview实现添加更新及鼠标移动事件
- swt为关闭窗口添加确认事件
- 为GridView添加鼠标事件
- 为GridView添加鼠标事件
- GridView添加删除确认
- 自定义GridView 模板中添加事件
- 给GridView分页的页码添加事件
- GridView/DataGrid 整行添加服务器事件
- 给GridView添加行单击事件
- GridView整行添加服务器事件
- GridView使用-添加事件在服务器端执行
- 自定义gridview添加行双击事件
- 给自定义的gridview添加双击事件
- 如何自己添加后台事件(Gridview双击事件)
- GridView 事件
- 教你玩转ACDSEE
- 网站界面设计:网页的均衡规划与选择
- Unix中sed的用法
- 创业成功必守36条铁律(四)
- 实训汇编语言设计——存储区数据传输
- gridview-----添加OnRowDeleting事件及确认事件OnRowCreated
- 位图进度条
- 自定义应用程序配置文件(app.config)
- 实训汇编语言设计——内存多字节10进制数相加
- CodeBlocks学习(四)中文支持
- 实训汇编语言设计——学生的成绩统计
- PHP将mysql数据表转换为excel文件
- 从今天开始
- 如何去掉Windows资源管理器和IE地址栏的自动补全功能