Vikita
Vikita

Reputation: 261

How to display a default image if Eval ("Thumbnail") is null - asp .net

I'm trying to upload a default thumbnail image if user hasn't uploaded any thumbnail for their news article. Can you please help?

I tried at first, but it didn't work:

<%#Eval("Thumbnail")!=null ? Eval("Thumbnail"):"~/Images/test.jpg"%>" alt="<%#Eval("Title") %>"

I'm have the following code in a repeater:

         <asp:Repeater ID="rptRotator" runat="server">
                    <HeaderTemplate>
                        <ul>
                    </HeaderTemplate>
                    <ItemTemplate>
                        <li>
                            <div class="widgetContent">
                                <img class="thumbNail" src="<%#Eval("Thumbnail") %>" alt="<%#Eval("Title") %>" />
                                <h4 style="width: 155px;">
                                    <a href="/news/<%#Eval("PublicationDate","{0:yyyy/MM/dd}")%>/<%#Eval("UrlName") %>">
                                        <%#Eval("Title") %></a></h4>
                                <div class="clear">
                                </div>
                                <span class="newsDate">
                                    <%# Eval("PublicationDate", "{0:dd MMMM yyyy}")%></span>
                                <div class="widgetTextSummary">
                                    <p>
                                        <%#Eval("Summary").ToString().Substring(0,60)%>...</p>
                                </div>
                            </div>
                        </li>
                    </ItemTemplate>
                    <FooterTemplate>
                        </ul></FooterTemplate>
                </asp:Repeater>

Upvotes: 2

Views: 5851

Answers (4)

Asif Iqbal
Asif Iqbal

Reputation: 543

<asp:Image ID="imgProd" runat="server" src="<%#Eval('Thumbnail')" 
onerror="this.onload = null; this.src='ImageurlORAnyImageHere.jpg';"/>

Upvotes: 0

James Johnson
James Johnson

Reputation: 46057

One of the following should work:

<%# Eval("Thumbnail") ?? "/images/default.png" %>

<%# Eval("Thumbnail") == DBNull.Value ? "/images/default.png" : Eval("Thumbnail") %>

Upvotes: 0

Paul Graffam
Paul Graffam

Reputation: 2149

A simple way to do this is to create a public method that you can use to determine if the data item is null or not and set it accordingly. This way you wouldn't have to put all your code inline. You would then have something like this in vb .net in your code behind:

Public Function ProcessDataItem(myDataItemValue As Object) As String
    If myDataItemValue Is Nothing Then Return "~/Images/test.jpg"

    Return myDataItemValue.ToString()
End Function

And then call it in the repeater:

<%# ProcessDataItem(Eval("Title")) %>" alt="<%#Eval("Title") %>"

Also, I recommend using Container.DataItem instead of Eval as it creates less overhead. So in the end it would be something like this:

<%# ProcessDataItem(Container.DataItem("Title")) %>" alt="<%#Container.DataItem("Title") %>"

Upvotes: 2

Wil P
Wil P

Reputation: 3371

How about give your property Thumbail a default value?

Then when the Thumbnail hasn't been set to a non-null value the default image will be displayed, otherwise the users thumbnail will be shown.

Upvotes: 0

Related Questions