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

How can I use a client certificate with a missing CA to communicate with a external Web service?
Registering and using forms with Windsor
Fast serialization/deserialization of structs
Sending UTF-8 Text Messages on C# using TIBCO EMS
Strategy, State, Singleton… or a Composition thereof?
Handling the events of a Interop control in C#
Regex to capture text around a literal
Why my TempData is not working
Fetching information from a database
data type mismatch in criteria expression in c# 2010
Skip DataMember via DataContractSerializer
ServicedComponent Com proxy eventually fails
Embedded scripting on the AppStore - use Python, or snake my way in?
Using Expression to call a property and object and determine if the object is null or not
LINQ joining values from different classes
wp7 xna gestures not working

Categories

HOME
oauth-2.0
ssms
botframework
microcontroller
bigtable
blob
highmaps
azure-resource-manager
thermal-printer
multipartform-data
qt4
restheart
revit-api
wkhtmltopdf
vichuploaderbundle
camunda
openstack-horizon
aiml
git-branch
angular2-pipe
fastboot
datastax-enterprise
eclipse-jdt
offset
n1ql
matlab-compiler
android-geofence
stdin
scalamock
armadillo
aspose.pdf
usernotifications
ninja-forms
head
gridgain
reqif
hdpi
lucee
skype-bots
ajv
nservicebus6
dynatable
acceleo
catalyst
scanf
lattice
key-value-store
bytearray
db2-zos
queuing
php-java-bridge
system-integration
boost-bind
oryx
videoview
google-app-engine-php
computability
ios8-share-extension
taverna
traminer
esb-toolkit-2.1
genome
httpmodule
autopep8
beagleboard
indices
os.walk
powershell-v1.0
javacompiler
azure-caching
sqldatasource
html5-filesystem
flask-mongoengine
php-amqplib
complete
forward-declaration
latex-suite
select2-rails
gmail-imap
setcookie
metapost
expresso-store
android-memory
umfpack
xpolog
moss2007-security
netbeans7.0
vista64
scalable

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