asp.net c# 对GridView编辑,删除,更新,取消的操作
程序员文章站
2023-12-28 09:06:10
...
GridView添加了编辑,删除,管理列后的操作
黑色头发:http://heisetoufa.iteye.com/
//编辑
protected void gvwOne_RowEditing(object sender, GridViewEditEventArgs e)
{
gvwOne.EditIndex = e.NewEditIndex;
gvwBand();//绑定
}
//更新
protected void gvwOne_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
String strOne = gvwOne.Rows[e.RowIndex].Cells[0].Text.Trim();
访问数据库等代码...
Response.Write("<script language:javascript>javascript:window:alert('修改成功');</script>");
gvwOne.EditIndex = -1;
gvwBand();//绑定数据
}
//取消
protected void gvwOne_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
gvwOne.EditIndex = -1;
gvwBand();//绑定
}
//删除
protected void gvwOne_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
String strOne = gvwOne.Rows[e.RowIndex].Cells[0].Text.Trim();
访问数据库等操作...
Response.Write("<script language:javascript>javascript:window:alert('删除成功');</script>");
gvwBand();//绑定
}
//删除时弹出提示框
protected void gvwOne_RowDataBound(object sender, GridViewRowEventArgs e)
{
//如果是绑定数据行
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
{
((LinkButton)e.Row.Cells[17].Controls[2]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除:\"" + e.Row.Cells[1].Text + "\"吗?')");//Cells[17]代表删除所在列索引,Controls[2]表示按下第三个汉字才弹出提示
}
}
//---------------鼠标经过行变色------------------
int i;
//执行循环,保证每条数据都可以更新
for (i = -1; i < gvwOne.Rows.Count; i++)
{
//首先判断是否是数据行
if (e.Row.RowType == DataControlRowType.DataRow)
{
//当鼠标停留时更改背景色
e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='red'");
//当鼠标移开时还原背景色
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
}
}
//---------------鼠标经过行变色------------------
}
黑色头发:http://heisetoufa.iteye.com/