为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
Hope it is usefull to you.....