2012-03-23 59 views
13

我有一個表單,用戶可以刪除記錄,並且我想要一個彈出消息,用戶必須單擊確定以確認刪除。ASP.NET在執行代碼隱藏之前進行確認

刪除按鈕:

<asp:Button ID="btnDelete" runat="server" Text="Delete" UseSubmitBehavior="false" OnClick="btnDelete_Click" OnClientClick="confirmation();" /> 

確認功能:

function confirmation() { 
     var answer = confirm("Are you sure you want to delete? This action cannot be undone.") 
    } 

所以現在,點擊刪除按鈕時執行的代碼中的btnDelete_Click子後面不管你是否點擊確定或取消彈出框。我知道我可以添加如果(答案){ - 這裏的一些代碼 - }在我的JavaScript函數,但是有可能使用JavaScript從代碼隱藏中執行代碼?還是有另一種方法來做到這一點?

+2

只需在用戶點擊取消時返回false,點擊確定時返回true。檢查出答案:Thit Lwin Oo – 2012-03-23 03:56:56

回答

30

請嘗試以下操作。您必須返回確認功能的結果(true或false)。

<asp:Button 
    ID="btnDelete" 
    runat="server" 
    Text="Delete" 
    UseSubmitBehavior="false" 
    OnClick="btnDelete_Click" 
    OnClientClick="return confirmation();" /> 

 

function confirmation() { 
    return confirm("Are you sure you want to delete?"); 
} 
+16

你可以'返回確認(...)' – Esailija 2012-03-23 03:59:07

+0

不起作用,仍然執行後面代碼中的btnDelete_Click Sub ... – Sara 2012-03-23 04:26:40

+1

@Sara它應該工作。我遇到過這個問題,關鍵是OnClientClick =「return JSFunction();」並且JSFunction返回true或false。如果爲false,則不會回傳。請再試一次。 – 2012-03-23 06:49:26

-2

如果你想的JavaScript代碼中訪問方法的背後,你可以總是一個RESTful服務揭露他們。

$.ajax({ 
    url: 'url to the rest call', 
    success: successfuntion(), 
    dataType: 'text/json' 
}); 

讓REST調用完成您需要的任何邏輯併發回一些數據以表示成功。然後,只需使用successfunction從DOM中刪除元素即可。

0

如果你想在服務器端執行一些代碼而不實際發回帖子,你必須使用ajax來做到這一點。

function confirmation() { 
     var answer = confirm("Are you sure you want to delete? This action cannot be undone.") 
     if(answer) 
     { 
      $.post("ajaxserverpage.aspx?item=34",function(data){ 
       alert(data); 
      }); 
     } 
     return false; 
    } 

,並有一個名爲頁面ajaxserverpage.aspx並在該頁面的頁面加載,檢查查詢字符串,執行你的相關的服務器端代碼,返回一些字符串

protected void Page_Load(object sender, EventArgs e) 
    { 
     if (Request.QueryString["item"] != null) 
     { 
      // read the item and do your stuff here 
     Response.Write("Deleted succssfully"); 
     Response.End(); 
     } 
    } 

您還可以使用generic handler (.ashx)做你的ajax服務器端處理,而不是.aspx文件。我更喜歡這種方法。

4

請使用此示例:

<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="return ConfirmOnDelete();"/> 

<script type="text/javascript">  
    function ConfirmOnDelete() { 
    if (confirm("Do you really want to delete?") == true) 
     return true; 
    else 
     return false; 
    } 
</script> 
9

在你的aspx代碼將這個:

OnClientClick="return confirm('Are you sure you want to delete this project?');" 
3

我們可以通過使用ConfirmButtonExtend呃,

<ajaxToolkit:ConfirmButtonExtender ID="cbeDelete" TargetControlID="btnDelete" ConfirmText="Are you sure you want to delete? This action cannot be undone." runat="server"> 

它非常簡單...

0

我覺得上面的,如果你不能夠與巴頓使用它,嘗試<asp:link button>應該工作。礦井工作得很好。

ASPX頁面: -

<asp:LinkButton ID="takeActionBtn" CausesValidation="false" runat="server" 
     onclientclick="return confirm('Do you really want to perform Action ?');"> Take Action </asp:LinkButton> 

VB服務器代碼隱藏: -

Protected Sub takeActionBtn_Click(ByVal sender As Object, ByVal e As EventArgs) Handles takeActionBtn.Click 

End Sub 

有一件事我注意的是,如果你是一個頁面,您可於使用控制(ASCX)需要在頁面或控制級別關閉/打開AutoEventWireup="false" <%@ control in <%@ page

祝您好運!

0

1)窗體設計

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<title></title> 
<script type = "text/javascript"> 
    function Confirm() { 
     var confirm_value = document.createElement("INPUT"); 
     confirm_value.type = "hidden"; 
     confirm_value.name = "confirm_value"; 
     if (confirm("Do you want to save data?")) { 
      confirm_value.value = "Yes"; 
     } else { 
      confirm_value.value = "No"; 
     } 
     document.forms[0].appendChild(confirm_value); 
    } 
</script> 
</head> 
<body> 
<form id="form1" runat="server"> 
    <asp:Button ID="btnConfirm" runat="server" 
OnClick = "OnConfirm" Text ="Raise Confirm" OnClientClick = "Confirm()"/> 
</form> 
</body> 
</html> 

2)代碼隱藏

public void OnConfirm(object sender, EventArgs e) 
{ 
string confirmValue = Request.Form["confirm_value"]; 
if (confirmValue == "Yes") 
{ 
this.Page.ClientScript.RegisterStartupScript(this.GetType(),"alert('You clicked YES!')", true); 
} 
else 
{ 
    this.Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('You clicked NO!')", true); 
} 
} 

3)當你點擊刪除按鈕確認框將顯示。

4)如果您單擊確定,然後確定部分將在其他的代碼沒有部分

5)在GRIDVIEW刪除按鈕同樣的方法工作。

3

我知道這是舊帖子,但你可以把上面的答案放到這樣的一行中。你甚至不需要寫這個函數。

<asp:Button runat="server" ID="btnDelete" Text="Delete" OnClick="btnDelete_Click" OnClientClick="if (!confirm('Are you sure you want to delete ? ')) return false;" /> 
+1

簡單而優雅,這也適用於我。代碼少於其他一些解決方案。 – 2017-07-13 14:52:23

0

你可以把:

onclientclick = "return (window.confirm('text message')); 

ASPX頁面,它做同樣的事情。