[英]How to add a "confirm delete" option in ASP.Net Gridview?
如何在 ASP.Net Gridview 中添加“確認刪除”選項?
這應該這樣做。
我在這里找到它: 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 任務,然后單擊編輯列。
在Selected fields 中選擇Delete ,然后單擊Convert this field into a TemplateField 。 然后點擊確定:
現在將生成您的LinkButton
。 您可以像這樣將OnClientClick
事件添加到LinkButton
:
OnClientClick="return confirm('Are you sure you want to delete?'); "
我這樣做有點不同。 在我的 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?");
});
這對於我需要做的事情來說既快速又簡單。 請注意,頁面中顯示 Delete 的每個 Anchor 都將被 jQuery 選中,並將事件添加到其中。
我喜歡這種在從 gridview 中刪除記錄之前添加確認提示的方式。 這是嵌套在 aspx 頁面中的 GridView Web 控件中的 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
這似乎工作正常。
我不想要任何圖像,所以我修改了@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。
OnClientClick="return confirm('confirm delete')"
嘗試這個:
我用於更新和刪除按鈕。 它不觸摸編輯按鈕。 您可以使用自動生成的按鈕。
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?');";
}
}
我非常喜歡在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));
}
}
盡管其中許多答案都有效,但這顯示了使用 OnClientClick 屬性在 GridView 中使用 CommandField 時的一個簡單示例。
ASPX:
<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?');";
}
}
我在獲取 commandField Delete 按鈕時遇到了問題,當用戶在使用 javascript confirm() 函數獲得的“您確定嗎”彈出窗口中單擊取消時,它會響應“返回假”響應。 我不想將其更改為模板字段。
在我看來,問題在於這些 commandField 按鈕已經有一些與之關聯的 Javascript 來執行回發。 簡單地附加confirm() 函數是無效的。
這是我解決它的方法:
使用JQuery,我首先在頁面上找到每個刪除按鈕(有幾個),然后根據訪問者是否同意或取消確認彈出來操作按鈕的關聯Javascript。
<script language="javascript" type="text/javascript">
$(document).ready(function() {
$('input[type="button"]').each(function() {
if ($(this).val() == "Delete") {
var curEvent = $(this).attr('onclick');
var newContent = "if(affirmDelete() == true){" + curEvent + "};"
$(this).attr('onclick',newContent);
}
});
}
function affirmDelete() {
return confirm('Are you sure?');
}
</script>
這段代碼對我來說很好用。
jQuery("a").filter(function () {
return this.innerHTML.indexOf("Delete") == 0;
}).click(function () { return confirm("Are you sure you want to delete this record?");
});
我喜歡 JavaScript 解決方案,並且有一些更新可以處理動態 ajax 加載:
$(document).on("click", "a", function () {
if (this.innerHTML.indexOf("Delete") == 0) {
return confirm("Are you sure you want to delete this record?");
}
});
希望它有幫助;)
這是我的方法,效果很好。
ASP
<asp:CommandField ButtonType="Link" ShowEditButton="true" ShowDeleteButton="true" ItemStyle-Width="5%" HeaderStyle-Width="5%" HeaderStyle-CssClass="color" HeaderText="Edit"
EditText="<span style='font-size: 20px; color: #27ae60;'><span class='glyphicons glyph-edit'></span></span>"
DeleteText="<span style='font-size: 20px; color: #c0392b;'><span class='glyphicons glyph-bin'></span></span>"
CancelText="<span style='font-size: 20px; color: #c0392b;'><span class='glyphicons glyph-remove-2'></span></span>"
UpdateText="<span style='font-size: 20px; color: #2980b9;'><span class='glyphicons glyph-floppy-saved'></span></span>" />
C#(用按鈕的列號替換5)
if ((e.Row.RowState & DataControlRowState.Edit) > 0)
{
}
else {
((LinkButton)e.Row.Cells[5].Controls[2]).OnClientClick = "return confirm('Do you really want to delete?');";
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.