4
<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.

Tony L.
  • 17,638
  • 8
  • 69
  • 66
Kings
  • 1,551
  • 12
  • 32
  • 52
  • I'm not familiar with the telerik controls, but I'm pretty sure what you want to do is specify the `ItemCommand` property for each button, then the `GridCommandEvent` will have a corresponding property, telling you which command was clicked. – asawyer Feb 27 '12 at 14:57

2 Answers2

4

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;
      }
 } 
Drew
  • 888
  • 7
  • 12
1

The source object is the one that fired the command. Just cast the source to the image button and check whether is the button1 or button2.

protected void RGStyleGuideRestrictions_ItemCommand(object source, GridCommandEventArgs e)
{
   ImageButton fired = source as ImageButton;
   if(fired!=null && fired.Id=="imgBtn1")
   {
      //imgBtn1 fired the command
   }
   else 
   {
     // and so on...
   }
   ImageButton imgBtn1 = e.item.FindControl("imgBtn1") as ImageButton;
   ImageButton imgBtn2 = e.item.FindControl("imgBtn2") as ImageButton;
}

UPDATE

Since code above didn't work, try this approach:

  <telerik:GridTemplateColumn UniqueName="TemplateColumn">
        <ItemTemplate>                
            <asp:ImageButton CommandArgument="btn1" ID="imgBtn1" runat = "server"/> 
            <asp:ImageButton CommandArgument="btn2" ID="imgBtn2" runat = "server"/>                     
        </ItemTemplate>
    </telerik:GridTemplateColumn>


protected void RGStyleGuideRestrictions_ItemCommand(object source, GridCommandEventArgs e)
{

   if(e.CommandArgument=="btn1")
   {
      //imgBtn1 fired the command
   }
   else if(e.CommandArgument=="btn2")
   {
      //imgBtn2 fired the command  
   }
   ImageButton imgBtn1 = e.item.FindControl("imgBtn1") as ImageButton;
   ImageButton imgBtn2 = e.item.FindControl("imgBtn2") as ImageButton;
}

Linking documentation to GridCommandEventArgs

Icarus
  • 63,293
  • 14
  • 100
  • 115
  • Icarus:- Its giving error --> 'fired' is NULL. I am clicking a above mentioned ItemButton only, not anything else. What could be the Problem >???? – Kings Feb 27 '12 at 15:17
  • @Kings try setting the CommandArgument property on each of the Image buttons and check e.CommandArgument to see whether btn1 or btn2 was fired. Will edit my answer to see if that helps. – Icarus Feb 27 '12 at 15:39
  • 'CommandName' attribute worked for me in an easier sense. But I spent time and tried to understand youe second approach. Thats worked with slight modification. Thanks alot for so much of effort. – Kings Feb 29 '12 at 10:15