2012-02-23 300 views
0

好吧我有一個gridview,當用戶點擊刪除鏈接按鈕回發時,允許用戶刪除一行。我得到按鈕觸發的行的索引,然後調用存儲過程來獲取計數。但在該行被刪除之前,會顯示確認信息。問題是用戶必須在出現確認對話框之前點擊兩次刪除鏈接按鈕。提前感謝您的幫助代碼如下。OnClientClick不會在第一次回發時觸發,但會在第二次點擊

<asp:GridView ID="updtCompGrdVw" runat="server" 
       AutoGenerateColumns="False" 
       DataKeyNames = "GUID" 
       OnRowCancelingEdit="updtCompGrdVw_RowCancelingEdit" 
       OnRowEditing="updtCompGrdVw_RowEditing" 
       OnRowUpdating="updtCompGrdVw_RowUpdating" 
       OnRowDeleting="updtCompGrdVw_RowDeleting" 
<RowStyle BackColor="White" /> 
    <EmptyDataRowStyle Wrap="False" /> 
    <Columns> 
    <asp:CommandField ShowDeleteButton="true" HeaderText="Delete" CausesValidation="false"> 
      <HeaderStyle ForeColor="White" /> 
      <ItemStyle HorizontalAlign="left" VerticalAlign="Top" Width="40px" Font-Bold="True" Font-Size="Small"/> 
       </asp:CommandField> 
</Columns> 
</asp:GridView> 

protected void updtCompGrdVw_RowDeleting(Object sender, GridViewDeleteEventArgs e) 
     { 
      try 
      { 
      int index = Convert.ToInt32(e.RowIndex); 
      DataKey dtKey = updtCompGrdVw.DataKeys[index]; 
      string DKpif_id = dtKey.Values["GUID"].ToString();// +", " + dtKey.Values["itmID"].ToString(); 
      GridViewRow Row = updtCompGrdVw.Rows[index]; 
      conn.Close(); 
      SqlCommand cmd3 = new SqlCommand("StoreProcedureName", conn); 
      cmd3.CommandType = CommandType.StoredProcedure; 
      cmd3.Parameters.Add("@GUID", SqlDbType.NVarChar); 
      cmd3.Parameters["@GUID"].Value = DKpif_id; 
      conn.Open(); 
      SqlDataAdapter adapter = new SqlDataAdapter(); 
      DataSet ds = new DataSet(); 
      adapter.SelectCommand = cmd3; 
      adapter.Fill(ds); 
      if (ds.Tables.Count > 0) 
      { 
       Session["CntctCount"] = ds.Tables[0].Rows[0]["ContactsCount"].ToString(); 

        (Row.Cells[Row.Cells.Count - 1].Controls[0] as LinkButton).OnClientClick = "if (!confirm('There are (" + Session["CntctCount"].ToString() + ") contacts associated with this company!, Are you sure you want to Delete this company?')) { return false; }"; 

        ds.Clear(); 
       conn.Close(); 
      } 
} 
      catch (SqlException ex) 
      { 

      } 
} 

回答

0

該事件的OnClientClick需要將已接線和JavaScript可在當用戶點擊刪除按鈕的時候 - 在Page_Load中,例如。 This link (MSDN)可能會幫助您瞭解頁面生命週期的概念,並且this link (MSDN)將幫助您解釋如何註冊您的腳本。

相關問題