This is a migrated thread and some comments may be shown as answers.

Change Image in Anchor on _ItemDataBound

2 Answers 214 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Eric Klein
Top achievements
Rank 1
Eric Klein asked on 03 Oct 2013, 09:50 PM
I have a grid and one of the cells is set as such

<rad:GridTemplateColumn AllowFiltering="False" UniqueName="EditLink" Groupable="false">
  <ItemTemplate>
    <a href="EditClient.aspx?ClientID=<%# Eval("ClientID")%>">
       <img alt="Edit Client" border="0" src="" title="Edit Client" />
   </a>
 </ItemTemplate>
 <HeaderStyle Width="20px" />
</rad:GridTemplateColumn>

What I am trying to do is set the image source based on a value in the grid at run time.

I assume ItemDataBound is the way to go but how do I get down to the image of the anchor in the item template


2 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 04 Oct 2013, 04:30 AM
Hi Eric,

Please try the following code snippet to access the Anchor tag and image in it.

ASPX:
<telerik:GridTemplateColumn DataField="OrderName" HeaderText="OrderName" UniqueName="OrderName">
    <ItemTemplate>
      <a href="FilterPosition.aspx?" id="link" runat="server">
       <img alt="Edit Client" border="0" src="" title="Edit Client" runat="server" id="img" />
      </a>
    </ItemTemplate>
    <HeaderStyle Width="20px" />
</telerik:GridTemplateColumn>

C#:
using System.Web.UI.HtmlControls;
 
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item is GridDataItem)
    {
        GridDataItem item = (GridDataItem)e.Item;     
        HtmlAnchor link = (HtmlAnchor)item.FindControl("link");//Access the anchor tag
        if (Your Condition)
        {
         HtmlImage img = (HtmlImage)item.FindControl("img"); //Access the image tag
         img.Src = "image.jpg";
        }
    }
}

Thanks,
Princy
0
Eric Klein
Top achievements
Rank 1
answered on 04 Oct 2013, 11:56 AM
Thanks I'll try that.  I did find that if I write html for the item["EditLink"].Text for the entire anchor and image it works also.
Tags
Grid
Asked by
Eric Klein
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Eric Klein
Top achievements
Rank 1
Share this question
or