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

How to update image control in grid cell

2 Answers 56 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Tomica
Top achievements
Rank 2
Tomica asked on 27 Aug 2012, 07:51 PM
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">
    <ItemTemplate>
           <asp:Image ID="targetControl" runat="server" ImageUrl="images/info16.gif" />
    </ItemTemplate>
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
        Try
            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
            Else
                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 Answers, 1 is accepted

Sort by
0
Accepted
Shinu
Top achievements
Rank 2
answered on 28 Aug 2012, 05:16 AM
Hi Tomica,

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

VB:
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

Thanks,
Shinu.
0
Tomica
Top achievements
Rank 2
answered on 28 Aug 2012, 03:17 PM
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.
Tags
Grid
Asked by
Tomica
Top achievements
Rank 2
Answers by
Shinu
Top achievements
Rank 2
Tomica
Top achievements
Rank 2
Share this question
or