<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事件。我想找出或获取代码隐藏中的ImageButton(1或2)的ID,它触发了ItemCommand。
请建议该怎么做。我不知道。
答案 0 :(得分: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;
}
}
答案 1 :(得分:1)
源对象是触发命令的对象。只需将源转换为图像按钮,然后检查是按钮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;
}
<强>更新强>
由于上述代码不起作用,请尝试以下方法:
<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