首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> .NET 網頁程式設計 >> ASP.NET 基礎 >> 關於ASP.NET

ASP.NET 2.0 GridView 伺服器端刪除確認實作

     編輯:關於ASP.NET

在 RowDataBound 事件中為刪除連結按鈕動態加入 JavaScript 確認對話框,並傳遞資料鍵值:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        LinkButton btnDelete = (LinkButton)e.Row.FindControl("LinkButton1");
        string id = DataBinder.Eval(e.Row.DataItem, "編號").ToString();
        btnDelete.Attributes.Add("onclick",
            "return confirm('確定要刪除編號 " + id + " 的記錄嗎?');");
    }
}

關鍵說明

  • 使用 DataBinder.Eval 取得當前資料列的主鍵值。
  • return confirm(...) 若使用者點取「取消」會回傳 false,中止 Postback。
  • 適用於 CommandName="Delete" 的 LinkButton 或 Button。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved