DataGrid前台代码:
<asp:ButtonColumn CommandName="Delete" Text="<div onclick="return confirm('真要删除吗?')">删除</div>" ItemStyle-Width="50px"></asp:ButtonColumn>
这里要注意Text="删除" 要改为Text="<div onclick="return confirm('真要删除吗?')">删除</div>"
后台代码:
protected void gvUserInfo_ItemDataBound(object sender, DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
LinkButton button = (LinkButton )e.Item.FindControl("Delete ");
button.Attributes.Add("onclick ", "return confirm( '确定要删除吗? '); ");
}
protected void gvUserInfo_DeleteCommand(object source, DataGridCommandEventArgs e)
{
DAL.UserInfo dUserInfo = new DAL.UserInfo();
int where = Convert.ToInt32(gvUserInfo.DataKeys[e.Item.ItemIndex].ToString());
dUserInfo.Delete(where);
page.ClientScript.RegisterClientScriptBlock(page.GetType(), "", "window.onload=function(){alert('" + "删除" + where.ToString() + "号用户成功"+ "');}", true);
UserInfoBind("delete");
}
GridView前台代码:
<asp:CommandField HeaderText="删除" ShowDeleteButton="True" />
Gridview后台代码:
protected void GridView_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[4].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除:\"" + e.Row.Cells[1].Text + "\"吗?')");
}
}
}
protected void GridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
//这里写删除语句
BindData();
}
本文介绍了如何在ASP.NET的DataGrid和GridView中实现删除操作前的弹窗确认功能。通过修改前台代码并添加后台处理逻辑,确保在进行数据删除之前用户会收到提示并进行二次确认。
2408

被折叠的 条评论
为什么被折叠?



