hide image if imageurl is blank

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gs430mw
    New Member
    • Apr 2010
    • 1

    hide image if imageurl is blank

    I am trying to hide an image in a formview if the imageurl is blank.

    Site is driven by database - and if there is nothing in the database as a picture filename then I want to hide the image - If there is a filename in the database I want to show the image.

    Showing is not a problem - but I dont want to show an empty placeholder box on the website if there is no file to display.

    Tried various methods to no avail.

    C# would be ideal as all my other code in C#

    Thanks in advance

    Code is below

    Code:
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:qbConnectionString %>" 
            SelectCommand="SELECT * FROM [tbl_PageInfo] WHERE ([type] = @type)">
      <SelectParameters>
        <asp:QueryStringParameter Name="type" QueryStringField="pageid" />
      </SelectParameters>
    </asp:SqlDataSource>
    
    <asp:FormView ID="FormView2" runat="server" DataSourceID="SqlDataSource1">
      <ItemTemplate>
        <asp:Image runat="server" 
            ID="titleimage" 
            ImageUrl='<%#Eval("picturefilename","http://bytes.com/images/{0}") %>' 
            alt='<%# Eval("para1title") %>' Width="200" Height="140"  />
        ...
        ..
      </ItemTemplate>
    </asp:FormView>
  • yarbrough40
    Contributor
    • Jun 2009
    • 320

    #2
    put this in the Page Load Event
    Code:
    if(Image1.ImageUrl == "")
    {Image1.Visible = false;}

    Comment

    • urbadave
      New Member
      • Apr 2010
      • 2

      #3
      You could change your select statement

      If your added another filter to the select statment in SqlDataSource1, you'd have an easier time.

      SELECT * FROM [tbl_PageInfo] WHERE ([type] = @type) AND ([picturefilename] > '')

      This way, empty picturefilename rows from the database don't end up in the data source you're binding to, so empty frames won't get displayed.

      Another suggestion would be that instead of
      SELECT *
      try
      SELECT [picturefilename], [para1title], [type]

      This can save some headaches down the road (don't ask me how I know).

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        From what I remember you can't set the Visible property based on the data that the control is bound to declaritively (you could do this perfectly fine if in your C# or VB.NET server code).

        I might be wrong, so the first thing you should try is setting the Visibility property depending on the data that the control is bound on.

        You can use the IIf Function (or the If Operator which short circuits if the first condition is false...unlike the IIf Function) in your binding.

        Try this (it may not work):
        Code:
        <asp:FormView ID="FormView2" runat="server" DataSourceID="SqlDataSource1">
          <ItemTemplate>
            <asp:Image runat="server" 
                ID="titleimage" 
                ImageUrl='<%#Eval("picturefilename","http://bytes.com/images/{0}") %>' 
                alt='<%# Eval("para1title") %>' 
                Width="200" 
                Height="140" 
                Visible="<%#If(DataBinder.Eval(Container.DataItem,"picturefilename")!="", True, False)  %>" />
        If that doesn't work you could set the CSS class for the image to a class so that the image is not displayed.

        You would have a CSS class that has the following style:
        Code:
        .hide{
          display:none;
        }
        And you would set the Image's CssClass according to the data it's bound to:
        Code:
        <asp:FormView ID="FormView2" runat="server" DataSourceID="SqlDataSource1">
          <ItemTemplate>
            <asp:Image runat="server" 
                ID="titleimage" 
                ImageUrl='<%#Eval("picturefilename","http://bytes.com/images/{0}") %>' 
                alt='<%# Eval("para1title") %>' 
                Width="200" 
                Height="140" 
                CssClass="<%#If(DataBinder.Eval(Container.DataItem,"picturefilename")!="", "","hide")%>" />
        -Frinny

        Comment

        • liawcv
          New Member
          • Jan 2009
          • 33

          #5
          Agree with what Frinny suggested. If compare against empty string (i.e. "") doesn't work, you may try to compare it against DBNull. Example:

          Code:
          <asp:Image ID="Image1" runat="server" 
              ImageUrl='<%# Eval("Picture", "~/Folder/{0}") %>' 
              Visible='<%# !(Eval("Picture") is DBNull) %>'
          />

          Comment

          • Frinavale
            Recognized Expert Expert
            • Oct 2006
            • 9749

            #6
            Did you try this code liawcv?

            I'm pretty sure that you'll run into a problem when you try to to use binding to set the Visible property of the Image control....but I'm not 100% sure on that so I'm curious. (I guess I should just try it myself and see what happens but maybe the OP will get back to us about what works and what doesn't)

            Regardless of whether or not setting the Visible property works, there are tons of ways to get around this problem :)

            -Frinny

            Comment

            • liawcv
              New Member
              • Jan 2009
              • 33

              #7
              Hi Frinny. Ya, I dare not post my codes here before I tried it out. It works : )

              Comment

              Working...