这应该可以解决问题。
我在这里找到了它:http://forums.asp.net/p/1331581/2678206.aspx
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:ImageButton ID="DeleteButton" runat="server" ImageUrl="~/site/img/icons/cross.png"
CommandName="Delete" OnClientClick="return confirm('Are you sure you want to delete this event?');"
AlternateText="Delete" />
</ItemTemplate>
</asp:TemplateField>
如果您的Gridview
使用了AutoGenerateDeleteButton="true"
属性,您可以将其转换为LinkButton
:
点击GridView任务,然后选择编辑列。
在选定字段中选择Delete,然后单击将此字段转换为TemplateField。 然后单击OK:
现在会生成您的LinkButton
。 您可以像这样向LinkButton
添加OnClientClick
事件:
OnClientClick="return confirm('您确定要删除吗?'); "
我的做法有点不同。在我的GridView中,我设置了 AutoGenerateDeleteButton="true"
。使用jQuery查找删除按钮并为找到的锚点添加点击事件。
jQuery("a").filter(function () {
return this.innerHTML.indexOf("Delete") == 0;
}).click(function () { return confirm("Are you sure you want to delete this record?");
});
对我需要做的事情来说,这非常快捷简单。只需注意,页面中显示“删除”的每个锚点都将被jQuery选择并添加事件。
我喜欢在gridview中删除记录之前添加确认提示的方式。这是aspx页面中嵌套在GridView web控件中的CommandField定义。这里没有什么花哨的东西,只是一个简单直接的Commandfield。
<asp:CommandField ShowEditButton="true" UpdateText="Save" ShowDeleteButton="True">
<ControlStyle CssClass="modMarketAdjust" />
</asp:CommandField>
然后,我只需要在GridView控件的RowDeleting事件中添加一些代码即可。这个事件在行实际被删除之前触发,这允许您获得用户的确认,并在他最终不想取消时取消事件。以下是我放置在RowDeleting事件处理程序中的代码:
Private Sub grdMarketAdjustment_RowDeleting(sender As Object, e As GridViewDeleteEventArgs) Handles grdMarketAdjustment.RowDeleting
Dim confirmed As Integer = MsgBox("Are you sure that you want to delete this market adjustment?", MsgBoxStyle.YesNo + MsgBoxStyle.MsgBoxSetForeground, "Confirm Delete")
If Not confirmed = MsgBoxResult.Yes Then
e.Cancel = True 'Cancel the delete.
End If
End Sub
看起来这样做很好。
<% %>
中不起作用。 - Siyon DP我不需要图片,因此我修改了@statmaster提供的答案,使它成为一个简单的输入,与其他列一起使用。
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="Delete" OnClientClick="return confirm('Are you sure you want to delete this entry?');">Delete </asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
使用Forecolor属性可以更改文本的颜色。
OnClientClick="return confirm('confirm delete')"
我很喜欢在 GridView
的 RowDataBound
事件中添加代码,以告知用户所尝试删除的确切项目。这会稍微提高用户体验?
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton lnkBtnDelete = e.Row.FindControl("lnkBtnDelete") as LinkButton;
// Use whatever control you want to show in the confirmation message
Label lblContactName = e.Row.FindControl("lblContactName") as Label;
lnkBtnDelete.Attributes.Add("onclick", string.Format("return confirm('Are you sure you want to delete the contact {0}?');", lblContactName.Text));
}
}
protected void gvOperators_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType != DataControlRowType.DataRow) return;
var updateButton = (LinkButton)e.Row.Cells[0].Controls[0];
if (updateButton.Text == "Update")
{
updateButton.OnClientClick = "return confirm('Do you really want to update?');";
}
var deleteButton = (LinkButton)e.Row.Cells[0].Controls[2];
if (deleteButton.Text == "Delete")
{
deleteButton.OnClientClick = "return confirm('Do you really want to delete?');";
}
}
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowDataBound="OnRowDataBound"... >
<Columns>
<!-- Data columns here -->
<asp:CommandField ButtonType="Button" ShowEditButton="true" ShowDeleteButton="true" ItemStyle-Width="150" />
</Columns>
</asp:GridView>
ASPX.CS:
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow && e.Row.RowIndex != GridView1.EditIndex)
{
(e.Row.Cells[2].Controls[2] as Button).OnClientClick = "return confirm('Do you want to delete this row?');";
}
}