Monday, December 17, 2012

How to Conditionally show any control in ASP.NET (Design View)




Most of the times we want to display an item from a database that may or may not have an associated download file link or image etc.

For example: you have different notices to be displayed on website, few of those notice records in database are having downloadble file (in db) and others may not have. and you want to display DOWNLOAD Link only where downloadble file exists in the database record.

If you bind the hyperlink to an control you can toggle the Visibility property from true to false, depending on the existence of an hyperlink, or image file path in the returned records. Following code can be used for the same.


C#:

Visible='<%# Eval("filePath")==DBNull.Value ? false: true %>'  


VB.Net



Visible=<%# IIf(Eval("filePath") Is DbNull.Value, "False","True") %>'




No comments: