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" />
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With