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

XmlElementAttribute not added by xsd.exe
Synchronize events in an Outlook calendar view
C# label text not change in method
How can I get checkboxes not in datagrid selected based on database value Y and N?
Deserialize Single Dimension JSON Array C#
Can Outlook Notes(memo) add Item on VSTO(C#)?
Return data from self loopoing method in C#
Add row in GridControl Devexpress onClick
Store static values into .net app [closed]
Manipulate with data in list - C#
WPF Bind Selected TabItem Background Property Programmatically
ASP NET - delete row using dataset in SQL
Why does the cursor disappear on combobox DroppedDown in c#
Json from a get API Call C#
c# mvc 5, facebook login - How to ask extra user info after initial login
How do I deal with INSERT parameters which may be null, but are also converted?

Categories

HOME
azure-documentdb
ldap
mysqli
blob
country-codes
ionic-native
kairosdb
adroitlogic
chef-recipe
github-flavored-markdown
sha1
web-worker
loss
keystone
descriptor
ppp
tftp
loopback
spring-oauth2
epub
asp.net-webhooks
ansible-inventory
rtsp
mongoid5
jslider
fisheye
gridgain
django-cors-headers
gige-sdk
boost-regex
blackberry
iban
cardview
stringbuilder
blackberry-webworks
mac-app-store
x12
qtruby
hfp
phpdesigner
window.location
abstract
opendolphin
app-data
android-togglebutton
httpmodule
veeam
service-broker
douglas-peucker
cordova-3
selenium-grid2
nest-initiative
xpand
debuggervisualizer
django-tests
rda
iwork
mongoid4
boofcv
autofilter
node-amqp
imaplib
svg-android
http-compression
qmainwindow
magicsuggest
boost.build
screensharing
motordriver
towerjs
automapper-2
zend-validate
mscorlib
discussion-board
isgl3d
lwp
quazip
cinder
nscharacterset
large-data-volumes
pixel-bender
high-load

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