【问题标题】:Get ID of the Control which fires ItemCommand in RadGrid获取在 RadGrid 中触发 ItemCommand 的控件的 ID
【发布时间】:2012-03-17 01:07:40
【问题描述】:
<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。

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

【问题讨论】:

  • 我不熟悉 Telerik 控件,但我很确定您要做的是为每个按钮指定 ItemCommand 属性,然后 GridCommandEvent 将具有相应的属性,告诉你点击了哪个命令。

标签: c# asp.net telerik radgrid itemcommand


【解决方案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

【讨论】:

  • Icarus:- 它给出的错误 --> 'fired' 为 NULL。我只单击上面提到的 ItemButton,而不是其他任何东西。可能是什么问题>????
  • @Kings 尝试在每个图像按钮上设置 CommandArgument 属性并检查 e.CommandArgument 以查看是否触发了 btn1 或 btn2。将编辑我的答案,看看是否有帮助。
  • 'CommandName' 属性对我来说更容易理解。但我花了时间并试图理解你的第二种方法。那是稍加修改。非常感谢您的努力。
【解决方案2】:

您是否尝试将 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;
      }
 } 

【讨论】:

  • @Kings 随时,很高兴它有帮助:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多