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

RadGrid gridtemplatecolumn Image Control assign image code behind

1 Answer 99 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Allan
Top achievements
Rank 2
Allan asked on 02 Dec 2013, 05:31 AM
I am attempting to assign the imageurl to the image control in the code behind.

I have it working somewhat however, I must not have the code looping correctly because if there is more than one item associated with the IDOrder, the grid displays images correctly unless there are two of the same types in the same IF statement. If there is more than one of the same type in any one IF statement then only the first image will show and the rest will simply display the dreaded X.

Here is my code.

Protected Sub rgd_OrderItems_ItemDataBound(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles rgd_OrderItems.ItemDataBound
 
    Dim IDOrder = Request.QueryString("IDOrder")
 
    Dim cn As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("ETP_OPS_ConnectionString").ToString)
    cn.Open()
    Dim SqlCmd As SqlCommand
    SqlCmd = New SqlCommand("SELECT ImageFilePath FROM vw_Orders_Items WHERE ([IDOrder] = @IDOrder)", cn)
 
    SqlCmd.Parameters.Add("@IDOrder", SqlDbType.NVarChar, 36).Value = IDOrder
 
    Dim ImageFilePath = CType(SqlCmd.ExecuteScalar, String)
    cn.Close()
 
    If TypeOf e.Item Is GridDataItem Then
 
        For Each item As GridDataItem In rgd_OrderItems.Items
 
            'Dim item As GridDataItem = DirectCast(e.Item, GridDataItem)
            Dim img As Image = DirectCast(item.FindControl("ImageFilePath"), Image)
 
            If ImageFilePath.Contains(".jpg") _
            Or ImageFilePath.Contains(".jpeg") _
            Or ImageFilePath.Contains(".gif") _
            Or ImageFilePath.Contains(".png") Then
 
                img.ImageUrl = ImageFilePath
 
            ElseIf ImageFilePath Like ("*.doc*") Then
 
                img.ImageUrl = "../../images/icon_word.gif"
 
            ElseIf ImageFilePath Like ("*.pdf*") Then
 
                img.ImageUrl = "../../images/icon_pdf.gif"
 
            Else
 
                img.ImageUrl = "../../images/icon_unknown.gif"
 
            End If
 
            'item("ImageFilePath").Attributes.Add("onclick", "OPenPopuP('" + ImageFilePath + "');")
        Next
    End If

Thank you in advance.

1 Answer, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 02 Dec 2013, 05:45 AM
Hello,

Please move your above code from ItemDataBound event to Grid/Page Prerender event.

Protected Sub RadGrid1_PreRender(sender As Object, e As EventArgs)
    Dim IDOrder = Request.QueryString("IDOrder")
  
    Dim cn As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("ETP_OPS_ConnectionString").ToString)
    cn.Open()
    Dim SqlCmd As SqlCommand
    SqlCmd = New SqlCommand("SELECT ImageFilePath FROM vw_Orders_Items WHERE ([IDOrder] = @IDOrder)", cn)
  
    SqlCmd.Parameters.Add("@IDOrder", SqlDbType.NVarChar, 36).Value = IDOrder
  
    Dim ImageFilePath = CType(SqlCmd.ExecuteScalar, String)
    cn.Close()
  
    If TypeOf e.Item Is GridDataItem Then
  
        For Each item As GridDataItem In rgd_OrderItems.Items
  
            'Dim item As GridDataItem = DirectCast(e.Item, GridDataItem)
            Dim img As Image = DirectCast(item.FindControl("ImageFilePath"), Image)
  
            If ImageFilePath.Contains(".jpg") _
            Or ImageFilePath.Contains(".jpeg") _
            Or ImageFilePath.Contains(".gif") _
            Or ImageFilePath.Contains(".png") Then
  
                img.ImageUrl = ImageFilePath
  
            ElseIf ImageFilePath Like ("*.doc*") Then
  
                img.ImageUrl = "../../images/icon_word.gif"
  
            ElseIf ImageFilePath Like ("*.pdf*") Then
  
                img.ImageUrl = "../../images/icon_pdf.gif"
  
            Else
  
                img.ImageUrl = "../../images/icon_unknown.gif"
  
            End If
  
            'item("ImageFilePath").Attributes.Add("onclick", "OPenPopuP('" + ImageFilePath + "');")
        Next
    End If
End Sub


Thanks,
Jayesh Goyani
Tags
Grid
Asked by
Allan
Top achievements
Rank 2
Answers by
Jayesh Goyani
Top achievements
Rank 2
Share this question
or