seting asp:image visibility only if file exists

  • Thread starter Thread starter mc
  • Start date Start date
M

mc

Scenario:

I have a database of companys, and a directory of images, each image has
the name of one of the companys, although not all companys have images.

I want a page which displays the details for each of the companys, so
far I'm using a repeater with labels for each of the address lines,
phone numbers, etc.

Problem:

I currently set the visibility of the entries from the database with the
following code: -

Visible='<%$!( (Eval("CompanyName") == System.DBNull.Value ) || (
((string)Eval("CompanyName")).Length == 0 ) %>'

Would there be a neater way of hiding the labels?

My main question though is is there a neat way to do the same for an
image, but instead of testing the DB, checking the file system for the
presence of a file?
 
mc said:
Scenario:

I have a database of companys, and a directory of images, each image has
the name of one of the companys, although not all companys have images.

I want a page which displays the details for each of the companys, so
far I'm using a repeater with labels for each of the address lines,
phone numbers, etc.

Problem:

I currently set the visibility of the entries from the database with the
following code: -

Visible='<%$!( (Eval("CompanyName") == System.DBNull.Value ) || (
((string)Eval("CompanyName")).Length == 0 ) %>'

Would there be a neater way of hiding the labels?

My main question though is is there a neat way to do the same for an
image, but instead of testing the DB, checking the file system for the
presence of a file?

how about this?

Visible =
'<%#System.IO.File.Exists(Server.MapPath("IMAGE_LOCATION"+"IMAGE_NAME"))%>'
 
Thanks for Mike's input.

Hi Mc,

For your scenario, if the databind expression will contains complex code
logic, generally I would consider use the following means instead of
directly embed all the code within inline <%# %> expression:

1. Define a helper function in the page's codebehind, put all the code
logic in that helper function and call this function in the inline
databinding expression. e.g:

suppose we define the following function in page's codebehind:
---- page class---------
protected bool IsVisible(int id)
{
//do some other things
return id % 3 == 0;
}
-------------------

In the repeater's template, you can use it as below:

<asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1">
<ItemTemplate>
<br /><hr /><br />
<asp:Image ID="Image1" runat="server" Visible='<%#
IsVisible((int)Eval("CategoryID")) %>'

ImageUrl="http://img.microsoft.com/downloads/img/products/C0037913-9E11-4A2D
-8FD1-0BA441296CBC.gif"/>
</ItemTemplate>
</asp:Repeater>

This would make the inline template much clearer.

2. All the template databound controls in ASP.NET suppose a Item/Row
Databound event which fires during after the databinding of each databind
item. Therefore, we can also use this event to customize any sub controls
in the ItemTemplate of the databound control. Here is the msdn reference on
Repeater control's "ItemDataBound" event.

#Repeater.ItemDataBound Event
http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.repeater.
itemdatabound.aspx

Hope this helps. If there is anything unclear, please feel free to let me
know.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.
 
I've used helper functions elsewhere, however, the issue I found using
helper functions was that, for my visibility statement I need to pass a
string to the function, if the string returned from the DB is null the
Cast fails?

Should I just pass the output of the eval as an Object and then attempt
the cast in the helper function?

Regards


Mike
 
Yes, good question.

I think using an object type paramter will be better for your case (the
return value from the database record could be null). You can even
directly pass the Container.DataItem into your function and use
DataBinder.Eval in your helper function if you do not mind that the helper
function become too tight coupled with the data access code logic. Which
one to use is up to the developer ;)

Please feel free to let me know if there is anything else we can help.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top