C# 获取在RadGrid中激发ItemCommand的控件的ID

C# 获取在RadGrid中激发ItemCommand的控件的ID,c#,asp.net,telerik,radgrid,itemcommand,C#,Asp.net,Telerik,Radgrid,Itemcommand,问题:-现在,单击任何一个ImageButton将触发ItemCommand事件。我想找出或获取codebehind中触发ItemCommand的ImageButton(1或2)的ID 请建议该怎么做。我没有线索。源对象就是触发命令的对象。只需将源代码转换为图像按钮,然后检查按钮是1还是2 protected void RGStyleGuideRestrictions_ItemCommand(object source, GridCommandEventArgs e) { ImageBut

问题:-现在,单击任何一个ImageButton将触发ItemCommand事件。我想找出或获取codebehind中触发ItemCommand的ImageButton(1或2)的ID


请建议该怎么做。我没有线索。

源对象就是触发命令的对象。只需将源代码转换为图像按钮,然后检查按钮是1还是2

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

由于上面的代码不起作用,请尝试以下方法:

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;
}


您是否尝试将命令名应用于图像按钮

  <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;
}

我不熟悉telerik控件,但我很确定您要做的是为每个按钮指定
ItemCommand
属性,然后
GridCommandEvent
将有一个相应的属性,告诉您单击了哪个命令。Icarus:-它的给定错误-->'fired'为空。我只点击了上面提到的一个项目按钮,而不是其他任何东西。可能是什么问题>??@Kings尝试在每个图像按钮上设置CommandArgument属性,并检查e.CommandArgument以查看是否触发了btn1或btn2。将编辑我的答案,看看这是否有帮助。“CommandName”属性在更简单的意义上对我有效。但我花了时间试图理解你的第二种方法。这一点稍加修改就行了。非常感谢您这么多的努力。@Kings,很高兴它能帮上忙:)
  <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;
}
 <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;
      }
 }