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>

在代码隐藏中:-

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

问题:-现在,单击任何 ImageButton 都会触发 ItemCommand 事件。我想在代码隐藏中找出或获取该 ImageButton(1 或 2)的 ID,它触发了 ItemCommand。

请建议为此做些什么。我不太清楚。

4

2 回答 2

4

您是否尝试过将 CommandNames 应用于您的图像按钮?

 <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;
      }
 } 
于 2012-02-27T15:25:40.187 回答
1

源对象是触发命令的对象。只需将源转换为图像按钮并检查是 button1 还是 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;
}

更新

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

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

将文档链接到GridCommandEventArgs

于 2012-02-27T14:56:34.050 回答