c#


Difficulty with conditional databinding


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?
<%# (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.
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 :)

Related Links

WCF Collection returns empty
Extending interface with new model
How to programmatically get Azure Service Bus Relay details (C#)?
The type or namespace name 'ThreadPoolTimer' could not be found
Join IQueryable with List on object from list
Exporting gridview with paging
Is it possible to pin a text on the top of the console c# [duplicate]
How to read simultaneously by several COM ports
How to delete rows with same value in datagridview
How to passing value to textbox in second form when object has been lunched
Make my datepicker required and remove it after selecting a date in c#
Microsoft Graph. DriveItem Id format?
DateTime parsing - unexpected result
Measuring mb sent per second with C# sockets? (Multithread)
C# inheritance multiple for different children
IList to JSON fails

Categories

HOME
oracle-sqldeveloper
compression
okhttp3
lithium
css-selectors
comments
promotions
ephesoft
distinct
javascript-debugger
yui
dompdf
jq
monad-transformers
datazen-server
cherrypy
ithit-webdav-server
chartist.js
sha
deployment-descriptor
hierarchical-data
marionette
sphinx4
pushpad
key-bindings
wordpress-rest-api
tikz
mplab
log4net-configuration
azure-vm-scale-set
stdin
lex
nscollectionview
ui-grid
wicket-tester
ape-phylo
superclass
wufoo
chrome-custom-tabs
spring-cloud-bus
jcs
appcmd
cancan
riemann
px4
wink
senti-wordnet
acceleo
alarmmanager
qudpsocket
turbolinks
zsh-completion
jspx
networkstream
particle-system
qtruby
php-java-bridge
camellia
pebble-js
periodic-processing
google-app-engine-php
encode
ios8-share-extension
angular-promise
tvp
pushwoosh
glog
genero
google-experiments
chunks
fxmlloader
qf-test
powerpoint-2010
fscanf
visual-c#-express-2010
sendy
sigma-grid-control
email-headers
xcode5.1
8-puzzle
angularjs-select2
mandelbrot
uikit-dynamics
reference-manual
mmo
freeimage
project-files
mcpd
recess
functional-specifications
hs-err
marathontesting
conditional-operator
nyromodal
web-widget
file-storage
rpxnow
retrospectiva
httpbrowsercapabilities
paperless

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App