Oct 28, 2013

Identify button Id in button click event handler

When you have multiple buttons more or like needed common handler, you can have the same handler for OnClick event like mentioned below.


<asp:LinkButton runat="server" ID="btnMonth" Text="Month" OnClick="btnView_Click" />

<asp:LinkButton runat="server" ID="btnWeek" Text="Week" OnClick="btnView_Click" />

<asp:LinkButton runat="server" ID="btnDay" Text="Day" OnClick="btnView_Click" />


Then, if needed you can find the button ID that user has clicked in the event handler like below.


 protected void btnView_Click(object sender, EventArgs e)
    {
      switch (((LinkButton)sender).ID)
      {
        case "btnWeek":
          // Do actions specific to btnWeek button click
          break;
        case "btnMonth":
          // Do actions specific to btnWeek button click
          break;
        case "btnDay":
          // Do actions specific to btnWeek button click
          break;
      }

      // Common Actions  
    }

No comments:

Post a Comment