Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

if statement in eval binding 1

Status
Not open for further replies.

stonehead

Technical User
May 2, 2007
58
US
Below is what I currently have. I'd like to put a if statement there to make my column show "N/A" if the field has null value. Please help me with the syntax. Thank you.

<ItemTemplate>

<asp:hyperlink navigateurl='<%#string.Concat ("\\serverName\FileRequest\" , Eval("fileName")) %>' text= '<%#Bind("fileName")%>' runat="server" id="link" Target="_blank"/>
</ItemTemplate>

 
I would do this in the code-behind in the ItemDataBound event of your control. Since the HyperLink is in a Template, you will need first to check that you are in the correct template, and then do a FindControl().
 
You can't put server script tags in the Text or Navigateurl property of asp:hyperlink.
If it's nescessary to use markup to bind the data, use the HTML href and <%= %> to fill in the "src".
Aside from that use : ResolveUrl(Eval("fileName")) or resolveclientUrl() for public sites to get the full path to your file.
To use if statements in the markup use "IIF(<condition>,<true>,<false>)" - VB.net Or
"(<Condition> ? <true> : <false> )" - C#. Mind you that VB.net evaluates the true statement even if the condition is false.
 
I strongly advise not to mix your code in the markup portion of the page. This is classic ASP style coding and makes for harder to read/maintain pages. Use the code behind file for any coding you need to do.
 
Beg to differ ... formview/objectdatasource pushes in the other direction. Using bindings and evals makes it ever so clear. Combined with good use of custom controls the pages are so much more readable. I hated having to fill up my textboxes and labels from code behind , always forgetting a few when changing the markup.
Everything has it's place... Don't use those IIF's too much though, they aren't advisable.
You could just expand your presentation-tier datatype with navigationurl.
 
formview/objectdatasource pushes in the other direction
Another reason why the datasource controls should be avoided.
amen!

another approach. move all the data access and logical transformations to outside of asp.net/webforms altogether and keep the UI as thin as possible. then you don't need to worry about binding events and other webforms "noise".
example:
1. fetch data from database
2. transform rows to view model object, including nulls to "n/a"
3. present the view model objects in the view (webform)

one (of many) issues with webforms is it actually complicates separation of concerns (discerning UI logic with business logic and data access).

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
Jason's advice is very good and should be taken. Separate the layers as much as possible.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top