RadGrid gridtemplatecolumn Image Control assign image code behind

2 posts, 0 answers
  1. Allan
    Allan avatar
    121 posts
    Member since:
    Jun 2008

    Posted 01 Dec 2013 Link to this post

    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.

  2. Jayesh Goyani
    Jayesh Goyani avatar
    2732 posts
    Member since:
    May 2010

    Posted 01 Dec 2013 Link to this post

    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
  3. UI for ASP.NET Ajax is Ready for VS 2017
Back to Top