Reputation: 57
I've been struggling to find some way to do my required conditional binding.
I want to use Eval("products_image")
in conditional binding in such a way that if product_image exists in images directory then it's ok, otherwise it should display "noimage.jpg".
I tried to do it this way:
<%# (File.Exists(("ProductImages/"+Convert.ToString(Eval("products_image"))))) ? ("ProductImages/"+Convert.ToString(Eval("products_image"))) : "ProductImages/noimage_small.jpg" ; %>
I have tried other ways as well, but every time, I mess up with a bunch of errors.
Can anyone guide me the right way to do this?
Upvotes: 0
Views: 369
Reputation: 57
I just moved the whole <script>
tag and System.IO
namespace inside the usercontrol .ascx file itself and it did it.
Thanks a ton configurator for help :)
Upvotes: 0
Reputation: 41620
<%# (File.Exists(("ProductImages/"+Convert.ToString(Eval("products_image"))))) ? ("ProductImages/"+Convert.ToString(Eval("products_image"))) : "ProductImages/noimage_small.jpg" ; %>
Quite long and unreadable, isn't it?
I'd suggest adding a method to your code behind or in a <script>
tag
// returns the imageFile parameter if the file exists, the defaultFile parameter otherwise
string ImageFileExists(string imageFile, string defaultFile) {
if (File.Exists(Server.MapPath(imageFile)))
return imageFile;
else
return defaultFile;
}
And then you'd simply use
<%# ImageFileExists("ProductImages/" + Eval("products_image").ToString(), "ProductImages/noimage_small.jpg") %>
Note that I've added a Server.MapPath
call to the method so that File.Exists
will actually look in the right place.
Upvotes: 1