Use alternate image field

I have created a small function to return an alternate image if the origional image field is empty as per the code below:

ImageUrl='<%# FixNullBind(Eval("PTProductImage")) %>'

Public Function FixNullBind(ByVal sItem As Object) As String
        If IsDBNull(sItem) Then
            '    Return ("/images/logo.jpg")
            Return ("<%# Bind(" + "" + "GFImageURL" + "" + ")%>")
            Return CStr(sItem)
        End If
    End Function

This works fine if I populate the return with the hardcoded image location, however I would like to use an image ("GFImageURL") in the same table that will always be populate even if the original ("PTProductImage")  isn't. In debugging I can  look at the return and it is poulated with
<%# Bind("GFImageURL")%> as I had planned but this is not being translated to the record withing this field.
Assistance most welcome.
Simon CrippsOwnerAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

Simon CrippsConnect With a Mentor OwnerAuthor Commented:
Solution found

ImageUrl='<%# FixNullBind(Eval("PTProductImage")Eval("GFImageURL"))  %>'

Public Function FixNullBind(ByVal sItem As Object, ByVal sDefault As Object) As String
        If IsDBNull(sItem) Then
                    Return (sDefault )
                End If
    End Function
Return("<%# Bind(" + GFImageURL +")%>")

Open in new window

Simon CrippsOwnerAuthor Commented:
I tried that origonally but it did not like GFImageURL as it was not declared.
I stuck with your solution and inserted
Dim GFImageURL As String
GFImageURL = "GFImageURL"
above. This again sets <%# Bind(GFImageURL)%> in the return but this is not interpreted in the ImageURL property.
 I thought it may just be missing quote marks arround GFImageURL. So recoded it as:
Return ("<%# Bind(" + Chr(34) + "GFImageURL" + Chr(34) + ") %>") which passed back the quotes but still did not give any value.  <%# Bind("GFImageURL")%>
 I have checked to ensure that GFImageURL is a field within the form and same table as PTProductImage.
where do you read the GFImageURL from ?
Simon CrippsOwnerAuthor Commented:
Below Is a cut down version of the page source to show the structure of the page. I have tried moving the SQLDatasource4 to next to SQLDatasource3 at the top of the page , however I get an error "Object reference not set to an instance of an object." in return on the repeater Item databound sub. This sub code is shown after the page source code below. The reference error source occours on line
ds.SelectParameters("PPTProductID").DefaultValue = pptProductID.ToString(). I don't know if this is the correct way to go, If you could look at this it would be great.
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
       <asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:acVitaConnectionString %>"

                <asp:SessionParameter ConvertEmptyStringToNull="false" DefaultValue="" Name="Brand" SessionField="SessionBrand" />
                <asp:SessionParameter ConvertEmptyStringToNull="false" DefaultValue="0" Name="MinPrice" SessionField="SessionMinPrice" />
                <asp:SessionParameter ConvertEmptyStringToNull="false" DefaultValue="160" Name="MaxPrice" SessionField="SessionMaxPrice" />
<div class="DivPageBody">        
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource3" OnItemDataBound="Repeater1_ItemDataBound">
  <asp:FormView DefaultMode="ReadOnly" ID="FormView4" runat="server" DataSourceID="SqlDataSource4" DataKeyNames="PPTProductID">
<div class="itemoffer" > 
<asp:Table ID="Table3" runat="server" Width="800px" Height="100px" >
<asp:TableRow Width="800px" >
<asp:TableCell Width="100px"> <asp:Image ID="ImageFile" runat="server" ImageUrl='<%# FixNullBind(Eval("PPTProductImage")) %>' Height="100px" Width="100px"  />
<asp:SqlDataSource ID="SqlDataSource4" runat="server" ConnectionString="<%$ ConnectionStrings:acVitaConnectionString %>" 
        SelectCommand="SELECT TOP (5) ProductTable.PPTProductID, ProductTable.PPTProductImage, GlobalFeed.GFImageURL, rest of SQL here">
<asp:SessionParameter Name="Brand" ConvertEmptyStringToNull="false" DefaultValue="" essionField="SessionBrand" />

Protected Sub Repeater1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles Repeater1.ItemDataBound

If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
Dim row As DataRowView = DirectCast(e.Item.DataItem, DataRowView)
Dim pptProductID As Integer = CInt(row.Row("PPTProductID"))
Dim fv As FormView = DirectCast(e.Item.FindControl("FormView4"), FormView)
Dim ds As New SqlDataSource
ds = DirectCast(e.Item.FindControl("SqlDataSource4"), SqlDataSource)
Dim ds1 As New SqlDataSource
ds1 = DirectCast(e.Item.FindControl("SqlDataSource3"), SqlDataSource)
ds.SelectParameters("PPTProductID").DefaultValue = pptProductID.ToString()
End If
End Sub

Open in new window

All Courses

From novice to tech pro — start learning today.