How to update image control in grid cell

3 posts, 1 answers
  1. Tomica
    Tomica avatar
    147 posts
    Member since:
    Apr 2008

    Posted 27 Aug 2012 Link to this post

    I have an ASP.NET image control in a templated column within RadGrid (lastest release)

    I have been able to adjust the background color of the cell as shown in the code below. However, the client prefers that I use customized icons that would vary based on several data conditions.

    I am hoping there is a way that I can access the ImageUrl property of the control in this cell.
    Note that this control differs from my color-coding example quoted below, but I think that whatever statement I need would fit into that overall logic.


    <telerik:GridTemplateColumn DataField="id"
        FilterControlAltText="Filter column column" HeaderText="Info"
        UniqueName="ID_column" SortExpression="id">
               <asp:Image ID="targetControl" runat="server" ImageUrl="images/info16.gif" />
    Protected Sub Event_Grid_ItemCreated(sender As Object, e As Telerik.Web.UI.GridItemEventArgs) Handles Event_Grid.ItemCreated
        Dim Temp As String = Nothing
        Dim Event_Date As Date = Nothing
        Dim End_Date As Date = Nothing
        If TypeOf (e.Item) Is GridDataItem Then
                Event_Date = CDate(e.Item.DataItem("event_date_sort"))
                End_Date = CDate(e.Item.DataItem("end_date"))
                Dim gdi As GridDataItem = CType(e.Item, GridDataItem)
                If End_Date < Today Then
                    gdi.Cells(2).BackColor = System.Drawing.Color.Silver
                ElseIf (End_Date >= Today) And (Event_Date <= Today) Then
                    gdi.Cells(2).BackColor = System.Drawing.Color.PaleGreen
                ElseIf Event_Date < DateAdd(DateInterval.Month, 1, Today) Then
                    gdi.Cells(2).BackColor = System.Drawing.Color.Yellow
                    gdi.Cells(2).BackColor = System.Drawing.Color.WhiteSmoke   
                End If
                If e.Item.DataItem("type_code") = "x" Then
                    gdi.Cells(2).BackColor = System.Drawing.Color.Tomato
                End If
            Catch ex As Exception
                'leave default shading
            End Try
        End If
    End Sub

  2. Answer
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 28 Aug 2012 Link to this post

    Hi Tomica,

    Try the following code snippet to access the ImageUrl property of the asp:Image.

    Protected Sub Radgrid1_ItemCreated(sender As Object, e As GridItemEventArgs)
        If TypeOf e.Item Is GridDataItem Then
            Dim ditem As GridDataItem = DirectCast(e.Item, GridDataItem)
            Dim img As Image = DirectCast(ditem.FindControl("targetControl"), Image)
            img.ImageUrl = "your url"
        End If
    End Sub

  3. DevCraft R3 2016 release webinar banner
  4. Tomica
    Tomica avatar
    147 posts
    Member since:
    Apr 2008

    Posted 28 Aug 2012 Link to this post

    Thank you very much.

    I have a general idea of what this code is doing and it works as given, except that I had to qualify "image" as "webcontrols.image" to resolve ambiguity.
Back to Top