您的位置:首页 > 其它

为GridView添加删除确认的若干种方法

2007-01-22 09:06 555 查看



先介绍最简单的方法

1.利用OnClientClick事件

<ItemTemplate>
<asp:ImageButton ID="IbnDel" runat="server" ToolTip="删除" CommandName="Delete" ImageUrl="/Images/delete01.gif" OnClientClick="javascript:return confirm('你确定要删除吗?将不可恢复哦');" />
</ItemTemplate>

2.后台代码里写,算最简便的一种方法了

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[8].Attributes.Add("onclick", "return confirm('删除此条记录?');");
}
}

3.遍历每个元素,找到你要的控件,添加删除确认.

protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
LinkButton l;
for(int i=0;i<e.Row.Cells.Count;i++)
{
for (int j = 0; j < e.Row.Cells[i].Controls.Count; j++)
{
if (e.Row.Cells[i].Controls[j].GetType().ToString() == "System.Web.UI.WebControls.DataControlLinkButton")
{
l = (LinkButton)e.Row.Cells[i].Controls[j];
if (l.Text == "删除")
{
l.Attributes.Add("onclick", "return confirm('确定要删除这个分类吗?');");
}
}
}
}
}

4.最后附上DataGrid的删除确认方法

private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
switch(e.Item.ItemType)//添加删除提示
{
case ListItemType.AlternatingItem:
case ListItemType.EditItem:
case ListItemType.Item:
{
LinkButton lbtDelete = (LinkButton)e.Item.Cells[2].Controls[0];
lbtDelete.Attributes.Add("onclick","return confirm('您确定要删除[" + e.Item.Cells[0].Text + "]的数据吗?此操作将删除[" + e.Item.Cells[0].Text + "]的所有小类以及所有新闻!');");
break;
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: