2010-06-08 87 views
0

如何從彈出的方法中彈出消息框以確認刪除?確認刪除記錄,從方法

通常我會在我的按鈕中使用以下行: OnClientClick =「return confirm('您確定要刪除此評論?');」

但是這個刪除方法也可以通過查詢字符串來調用,所以我需要在方法中確認消息框的功能嗎?

// delete comment method 
private void DeleteComment() 
{ 

      int commentid = Int32.Parse(Request.QueryString["c"]); 

      // call our delete method 
      DB.DeleteComment(commentid); 

} 

我無法通過代碼按一下按鈕,因爲它不火的OnClientClick事件 btnDelete_Click(NULL,NULL);

問候

熔體

回答

0

我會推薦做這樣的事情。

添加第二個查詢字符串參數來決定是否確認它。絕對添加一些代碼來確認用戶已經登錄,這樣這個查詢字符串方法不會被webcrawler或其他東西擊中,並且意外刪除所有的註釋。

// delete comment method 
private void DeleteComment() 
{ 

    if(Boolean.Parse(Request.QueryString["confirm"]) 
    { 
      int commentid = Int32.Parse(Request.QueryString["c"]); 

      // call our delete method 
      DB.DeleteComment(commentid); 
    } 
    else 
    { 
      ScriptManager.RegisterStartupScript(this, this.GetType(), "ConfirmDelete", @" 
      if(confirm('Are you sure you want to delete this comment?')) 
       window.location = '[insert delete location with confirm set to true]';     
      ", true); 
    } 

} 
1

您可以嘗試使用此

ibtnDelete.Attributes.Add("onClick", "javascript:return confirm('Are you sure you want to delete ?');"); 
+0

嗨,這不會導致頁面加載時觸發onClientClick事件。 感謝您的回覆 關注 Melt – Melt 2010-06-08 16:38:32

0

聽起來像你需要檢查查詢字符串用JavaScript,然後顯示你的提示。
看看這個post

你可以修改方法,並在body.onload或(jquery).ready函數中調用它。那麼問題就變成了如何讓你的服務器端方法知道結果?您可以設置一個單獨的頁面來處理刪除並使用jquery ajax post方法調用它。

我想這是我的.02