BinaryImage setting ImageUrl if DataValue is not null fails

2 posts, 1 answers
  1. Timo
    Timo avatar
    5 posts
    Member since:
    Jan 2011

    Posted 08 Jul 2011 Link to this post


    I have a database where the user may upload an alternative image for a device.
    If the image is not present in the database, the datasource returns a DBNull and the binary image should show the standard image file from the servers image directory.
    I used this code which works fine if the image is not present.

    <telerik:RadBinaryImage ID="RadBinaryImage1" runat="server"
       DataValue='<%# Eval("Data") is DBNull ? null : Eval("Data")%> '
    AlternateText=" " AutoAdjustImageControlSize="true" 
       ImageUrl='<%# Eval("Type", "~/Images/Devices/{0}_sml.png") %>'
    Width="64px" Height="64px" ImageAlign="Middle" ResizeMode="Fit" />

    If the database returns the image I get this error message during the page creation:
    "Cannot set image Url if DataValue is already set"

    If I remove the ImageUrl property, the image of the database is shown properly. So it's surely not a corrupted image file.
    I am currently using the following workaround:
    <telerik:RadBinaryImage ID="DeviceBinaryImage" runat="server"
        DataValue='<%# Eval("Data") is DBNull ? null : Eval("Data")%> '
    AlternateText=" " AutoAdjustImageControlSize="true"
        Visible='<%# Eval("Data") is DBNull ? false : true %>'
    Width="64px" Height="64px" ImageAlign="Middle" ResizeMode="Fit" />
     <asp:Image ID="Image1" runat="server" 
            ImageUrl='<%# Eval("Type", "~/Images/Devices/{0}_sml.png") %>' 
        Visible='<%# Eval("Data") is DBNull ? true : false %>' />

    Is it possible to set DataValue and the alternative ImageUrl Property of a single RadBinaryImage?
    I would expect the RadBinaryImage to ignore the ImageUrl if DataValue has data.

    Forget about the thread.
    Simply swapping the order of ImageUrl and DataValue did the trick. :)

  2. Answer
    Mira avatar
    1124 posts

    Posted 13 Jul 2011 Link to this post

    Hello Timo,

    Generally, it is not recommended to set the DataValue and the ImageUrl properties of the RadBinaryImage at the same time.
    In order to implement the desired functionality, I recommend that you handle the ItemDataBound event of the control and set the ImageUrl only if the DataValue is null.

    I hope this helps.

    the Telerik team

    Register for the Q2 2011 What's New Webinar Week. Mark your calendar for the week starting July 18th and book your seat for a walk through of all the exciting stuff we will ship with the new release!

  3. UI for ASP.NET Ajax is Ready for VS 2017
Back to Top