seting asp:image visibility only if file exists

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?
 
M

Mike

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"))%>'
 
S

Steven Cheng[MSFT]

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.
 
M

mc

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
 
S

Steven Cheng[MSFT]

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.
 
Joined
Mar 16, 2007
Messages
1
Reaction score
0
ItemDataBound event

Hi,
You can solve your problem with ItemDataBound event of repeater control only. This event is called whenever an item is bound to the repeater. So You can chek for the field from the databse for Imgae, and if it is null, just make the visible=False property of the image control or label control you are suing.

eg:
Dim limgNews As New System.Web.UI.WebControls.Image

limgNews = CType(e.Item.FindControl("myImageId"), System.Web.UI.WebControls.Image)

If limgNews IsNot Nothing Then
If limgNews.ImageUrl = "" Then
limgNews.Visible = False
End If
End If

You can check the way above or limgNews.ImageUrl = DBNull.Value. You have to write this code in itemdatabound event.

Thanks,
Mehul
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top