0

好的,我有一个允许用户删除一行的 gridview,当用户单击删除链接按钮时,会发生回发。我得到按钮触发的行的索引,然后调用一个获取计数的存储过程。但在删除该行之前,将显示确认信息。问题是用户必须在确认对话框出现之前单击两次删除链接按钮。提前感谢您的帮助代码如下。

<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)
            {

            }
}
4

1 回答 1

0

OnClientClick 事件需要已经连接,并且在用户单击删除按钮时 JavaScript 可用 - 例如在 Page_Load 中。 此链接 (MSDN)可以帮助您了解页面生命周期的概念,并且此链接 (MSDN)将帮助解释如何注册您的脚本。

于 2012-02-23T16:26:40.100 回答