簡體   English   中英

獲取在RadGrid中觸發ItemCommand的Control的ID

[英]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>

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

問題: -現在,單擊任何ImageButton會觸發ItemCommand事件。 我想找出或獲取codebehind中的ImageButton(1或2)的ID,它觸發了ItemCommand。

請建議該怎么做。 我比較清楚。

您是否嘗試過將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;
      }
 } 

源對象是觸發命令的對象。 只需將源轉換為圖像按鈕,然后檢查是按鈕1還是按鈕2。

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

由於上面的代碼不起作用,請嘗試以下方法:

  <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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM