Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which function will execute first in asp.net?

I want to know which Function will execute first OnCommand or OnClientClick ?

<asp:LinkButton ID="btnHRApprove" runat="server" Text="Approve" OnCommand="btnApprove_Click" OnClientClick="return ValidatePayrate()" CommandArgument="HR" CssClass="adLink" />
like image 833
Dilip Nayak Avatar asked Aug 05 '26 11:08

Dilip Nayak


2 Answers

 OnClientClick="return ValidatePayrate()" 

This will execute first, based on its return value next function will execute, if it returns false then further execution stops otherwise

OnCommand="btnApprove_Click"

will execute.

like image 129
Kuldeep Kumawat Avatar answered Aug 07 '26 02:08

Kuldeep Kumawat


OnClientClick

executes first since it is executed on the client side, but is bound to only one controller, while

OnCommand

is also executed on client side but calls a server sided method and can be bound to several buttons with different Id:s, and then handled through a switch case to determine which the command is like:

  <asp:Button id="btnHRApprove"
       Text="approve"
       CommandName="ValidatePayrate"
       OnCommand="btnApprove_Click" 
       runat="server"/>

  <asp:Button id="btnSubmit"
       Text="Submit"
       CommandName="Submit"
       OnCommand="btnApprove_Click" 
       runat="server"/>

Then in the code behind(in this case C#), you can use a switch case to check which command is given rather than which button was clicked:

  void btnApprove_Click(Object sender, CommandEventArgs e) 
  {
     switch(e.CommandName)
     {
        case "ValidatePayrate":
           //Your ValidatePayRate code here
           break;
        case "Submit":
           // Submit code here
           break;
        default:
           // Default
           break; 
     }
  }

If the client has javascript turned off, the OnClientClick will not fire and the OnCommand will fire instead

like image 38
Mikael Puusaari Avatar answered Aug 07 '26 00:08

Mikael Puusaari



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!