Search code examples
c#asp.nettelerikradgriditemcommand

Get ID of the Control which fires ItemCommand in RadGrid


<telerik:RadGrid ID="RGStyleGuideRestrictions" runat="server" DataSourceID="SqlDataSource1"
                OnItemCommand="RGStyleGuideRestrictions_ItemCommand"

    <MasterTableView DataSourceID="SqlDataSource1" DataKeyNames="TerritoryReportGroup">

      <Columns>
        <telerik:GridTemplateColumn UniqueName="TemplateColumn">
            <ItemTemplate>                
                <asp:ImageButton ID="imgBtn1" runat = "server"/> 
                <asp:ImageButton ID="imgBtn2" runat = "server"/>                     
            </ItemTemplate>
        </telerik:GridTemplateColumn>
      </Columns>
    </MasterTableView>
</telerik:RadGrid>

In CODE-BEHIND:-

protected void RGStyleGuideRestrictions_ItemCommand(object source, GridCommandEventArgs e)
{
   ImageButton imgBtn1 = e.item.FindControl("imgBtn1") as ImageButton;
   ImageButton imgBtn2 = e.item.FindControl("imgBtn2") as ImageButton;
}

QUESTION:- Now, click of any of the ImageButton fires the ItemCommand event. I want to find out or fetch the ID of that ImageButton(1 or 2) in codebehind, which fired the ItemCommand.

Please suggest what to do for that. I am clue less.


Solution

  • Have you tried applying CommandNames to your imagebuttons?

     <asp:ImageButton ID="imgBtn1" runat = "server" CommandName="imgAction1"/> 
     <asp:ImageButton ID="imgBtn2" runat = "server" CommandName="imgAction2"/> 
    

     protected void RGStyleGuideRestrictions_ItemCommand(object source, GridCommandEventArgs e)
     {
          switch(e.CommandName)
          {
             case "imgAction1": // do stuff here
                 break;
             case "imgAction2": // do some other stuff here
                 break;
          }
     }