您的位置:首页 > 其它

为Gridview的CommandField删除添加"是否确认删除?"

2009-08-01 16:51 393 查看

为Gridview的CommandField删除添加"是否确认删除?"

为Gridview的CommandField删除添加"是否确认删除?" 在网上的方法很多,,也可以用,,
例如以下::
首先,在GridView的属性对框话框中点击“Columns”进入它的“字段”设计器。接着在“字段”设计器中选择以前已加上的那个CommandField“删除”列,这时在它的属性列表下会看到一个“将此它段转换为 TemplateFied”的项,点击将它转换为TemplateFied列。

完后退出该字段设计器,切换到源码视图你会发现该列已由原来的:<asp:CommandField ShowDeleteButton="True" />
变为了:
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Delete" Text="删除"></asp:LinkButton>
</ItemTemplate>

最后在<asp:LinkButton>中加入:OnClientClick="return confirm('确认要删除吗?');"

这样点击删除时就会先在客户端弹出“确认要删除吗?”对话框,而原来在RowDeleting事件中写的代码完全不用改变。

在绑定方法里面写
for(int i;i<写你自己的GridView名称.Rows.Count;i++)
{
LinkButton lb=(LinkButton)写你自己的GridView名称.Rows[i].Cells[这里写你的索引值比方说在删除的那个按钮在第二列就写2].Controls[0你的GridView是第几个GridView控件就写几我先应该只有一个就写0吧];
lb.Attributes.Add("onclick","return confirm('真的要删除吗?')")
}

以下是我写的代码示例
/// <summary>
/// 绑定商品信息
/// </summary>
public void SpxxBD()
{
SelectStockService selservice = new SelectStockService();
GVspxx.DataSource = selservice.SelectGoodsInfo("");
GVspxx.DataBind();
for (int i = 0; i < GVspxx.Rows.Count; i++)
{
LinkButton linkbtn = (LinkButton)GVspxx.Rows[i].Cells[9].Controls[0];
linkbtn.Attributes.Add("onclick", "return confirm('真的要删除该货品吗?');");
}
}

转换成template

你在智能标记中选择 编辑列,你是否 有看到弹出窗口的右下角有一个"将此字段转换为TEMPLATEFIELD 的字样 ",你点那个链接后,原来的commandfield能实现的事情,它都可以帮你完成.. 然后你在template field中 只要加上 onClientClick="return confirm ('are you sure you want to delete this record?');" 即OK~...

Hope it is usefull to you.....
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐