OnClientClick不会在第一次回发时触发,但会在第二次点击时触发

时间:2012-02-23 16:05:56

标签: c# gridview postback onclientclick rowdeleting

好的我有一个允许用户删除行的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)
            {

            }
}

1 个答案:

答案 0 :(得分:0)

OnClientClick事件需要已经连线,并且当用户点击删除按钮时,javascript可用 - 例如,在Page_Load中。 This link (MSDN)可以帮助您了解页面生命周期的概念,this link (MSDN)将有助于解释如何注册脚本。