Reputation: 1591
<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>
In 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;
}
QUESTION:- Now, click of any of the ImageButton fires the ItemCommand event. I want to find out or fetch the ID of that ImageButton(1 or 2) in codebehind, which fired the ItemCommand.
Please suggest what to do for that. I am clue less.
Upvotes: 4
Views: 9663
Reputation: 63970
The source object is the one that fired the command. Just cast the source to the image button and check whether is the button1 or 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;
}
UPDATE
Since code above didn't work, try this approach:
<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;
}
Linking documentation to GridCommandEventArgs
Upvotes: 1
Reputation: 898
Have you tried applying CommandNames to your imagebuttons?
<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;
}
}
Upvotes: 4