locked
ASP.net Button click event firing twice sometimes RRS feed

  • Question

  • User-769511825 posted


    Hello Friends ,
    I have Button a inside gridview some times  Button click event firing twice not every time . Suppose 700 records inserted in data then 50 are repeated. i have checked everything but found nothing  .I don't know why its firing twice or more then that in single click sometime . i have also disabled thru client click but still going same thing .
    Here is my code:

          protected void btn_transfer_Click(object sender, EventArgs e){
          
    
            
    
    string user_id="1";
            GridViewRow gr = (GridViewRow)((Button)sender).Parent.Parent;
            TextBox txt_amt = (TextBox)gr.FindControl("txt_Amt_main");
            decimal amount_entered = 0;
    
            if (decimal.TryParse(txt_amt.Text, out amount_entered))
            {
                amount_entered = Convert.ToDecimal(txt_amt.Text);
            }
            amount_entered = amount_entered / 2;
            decimal cur_val = 15;
    
            string id = grd_withdrawal.DataKeys[gr.RowIndex].Values["Id"].ToString();
    
            string incom_per = grd_withdrawal.DataKeys[gr.RowIndex].Values["incom_per"].ToString();
            DataTable dt_user_wid_check = d.FillDataTable("SELECT typ FROM bl_r WHERE  user_id='" + user_id + "'");
            if (dt_user_wid_check.Rows.Count > 0)
            {
                d.executenonquery("update User_inc set inc_dis='Paid' where User_inc .id='" + id + "'");
                d.messagebox("return msg..", this);
                fill_grd();
                return;
            }
            string gid = Guid.NewGuid().ToString();
            string query = @"
    begin tran  T1
    BEGIN TRY  
    
    update User_inc set inc_dis='Paid' where User_inc .id='" + id + @"'
    
      insert into[tab_wt]
                    (ins_date,
                    user_id,
                    coin_rate,
                    coin_val_dr,
                    coin_val_cr,
                    purchase_from,
                    staus,
                    type, referance)values(
                    dateadd(hh,2, cast(SWITCHOFFSET(TODATETIMEOFFSET(getdate(),'-05:00'),'+05:30') as datetime)),
                    @user_id,
                    @coin_rate,
                    @coin_val_dr,
                    @coin_val_cr,
                    @purchase_from,
                    @staus,
                    @type,@referance)
    
    COMMIT TRAN T1;  
    END TRY
    BEGIN CATCH
     ROLLBACK TRAN T1
    END CATCH    ";
    
            SqlCommand cmd = new SqlCommand(query, cn);
            cmd.Parameters.AddWithValue("@user_id",user_id);
            cmd.Parameters.AddWithValue("@coin_rate", cur_val);
            cmd.Parameters.AddWithValue("@amt_dr", amount_entered);
            cmd.Parameters.AddWithValue("@coin_val_dr", "0");
            cmd.Parameters.AddWithValue("@coin_val_cr", (amount_entered * cur_val).ToString());
            cmd.Parameters.AddWithValue("@purchase_from", "tnvu");
            cmd.Parameters.AddWithValue("@staus", "Valid");
            cmd.Parameters.AddWithValue("@type", "Purchase");
            cmd.Parameters.AddWithValue("@referance", gid);
            cn.Open();
            cmd.CommandTimeout = 1000;
            int icheck = cmd.ExecuteNonQuery();
            cn.Close();
    
    }

    <asp:GridView runat="server" ID="grd_withdrawal" AutoGenerateColumns="false" DataKeyNames="Id,incom_per" CssClass="table tab-content table-bordered table-hover">
                                                        <Columns>
                                                            <asp:TemplateField HeaderText="Sr.">
                                                                <ItemTemplate>
                                                                    <%# Container.DataItemIndex+1 %>
                                                                </ItemTemplate>
                                                            </asp:TemplateField>
                                                            <asp:BoundField DataField="incom_per" HeaderText="Level" />
                                                            <asp:BoundField DataField="Amt_main" HeaderText="Amount" />
                                                            <asp:TemplateField HeaderText="Action">
                                                                <ItemTemplate>
                                                                    <asp:TextBox ID="txt_Amt_main" runat="server" Text='<%# Eval("Amt_main") %>' CssClass="form-control" Visible="false" Enabled="false"></asp:TextBox>
                                                                    <asp:Button ID="btn_transfer" runat="server" Text="transfer" OnClick="btn_transfer_Click" OnClientClick="this.disabled=true;" />  
                                                                </ItemTemplate>
                                                            </asp:TemplateField>
                                                        </Columns>
                                                    </asp:GridView>


    Wednesday, January 17, 2018 6:29 PM

All replies

  • User-707554951 posted

    Hi amangpt145

    Try to add UseSubmitBehavior attribute to asp:Button

    UseSubmitBehavior="false" 
    OnClientClick="this.disabled='true' ; this.value= 'Please Wait..';"

    Best regards

    Cathy

    Thursday, January 18, 2018 9:34 AM
  • User-769511825 posted

    Hi Cathy Zou,
    I have tried UseSubmitBehavior="false"  but that's not worked , still same thing happening some times function executing many times. 

    Friday, January 19, 2018 9:03 AM
  • User533502567 posted

      <asp:Button ID="btn_transfer" runat="server" Text="transfer" OnClick="btn_transfer_Click" OnClientClick="this.disabled=true;" />  

    May be you have duplicate event handler in code behind.  Try by removing "onClick" from aspx..

      <asp:Button ID="btn_transfer" runat="server" Text="transfer" OnClientClick="this.disabled=true;" />  

    Saturday, January 20, 2018 2:45 AM