How to determine which button caused postback

Will picture Will · Apr 19, 2012 · Viewed 17.2k times · Source

I have 2 button controls. When I click one i'm trying to determine which one caused a postback in the page load. How to do determine this?

Answer

Kris Krause picture Kris Krause · Apr 19, 2012

What about using CommandName and CommandArgument has shown in this example. This way you can have just one handler.

<asp:Button id="Button1"
       Text="Sort Ascending"
       CommandName="Sort"
       CommandArgument="Ascending"
       OnCommand="CommandBtn_Click" 
       runat="server"/>

  <asp:Button id="Button2"
       Text="Sort Descending"
       CommandName="Sort"
       CommandArgument="Descending"
       OnCommand="CommandBtn_Click" 
       runat="server"/>