Change Image in Anchor on _ItemDataBound

3 posts, 0 answers
  1. Eric Klein
    Eric Klein avatar
    183 posts
    Member since:
    Aug 2009

    Posted 03 Oct 2013 Link to this post

    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. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 03 Oct 2013 Link to this post

    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
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Eric Klein
    Eric Klein avatar
    183 posts
    Member since:
    Aug 2009

    Posted 04 Oct 2013 Link to this post

    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.
Back to Top