More Group Sites
Education Books
School Rankings
Jobless Net
Better Home
Enviro++
更好教育论坛


Help | Subscribe/Unsubscribe | Rules | Other Group Sites: Better Education | Better Education Forum
Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

Share
Options
View
Go to last post Go to first unread
hong  
#1 Posted : Friday, 2 October 2015 8:16:44 PM(UTC)
hong

Rank: Administration

Reputation:

Groups: AcademicCoachingSchool, admin, Administration, BookSeller, CatholicSchool, CoachingAdult, CoachingProfessional, CoachingSports, ExtraCurriculumCoaching, IndependentSchool, Moderator, MusicTeacher, PrivateSchool, PublicSchool, SelectiveSchool, tutor
Joined: 23/11/2008(UTC)
Posts: 519

ASP.NET DataBinder Eval examples

1. Dynamically enable a control based on datasource value
The following formats are correct.
Enabled='<%# Eval("FeatureLink")=="Y" ? true : false %>'
Enabled='<%# Eval("FeatureLink", "{0}").ToLower()=="y" ? true : false %>'
Enabled='<%# Eval("FeatureLink").Equals("Y") %>'

You need to check whether the eval is null before testing it if there is a null object reference exception.
Enabled='<%# Eval("FeatureLink")==null ? false : Eval("FeatureLink").Equals("Premium") %>'

Note that Enabled="<%# false %>" should be simplified as Enabled="false" in spite of no syntax error.

Two points here: 1) Eval must be in single quotes; 2) boolean type true or false must be used if using C# ternary syntax; You can use == or Equals to compare strings. This is different from "== here will not compare the string contents, but the string objects. So, instead, you must use .Equals()" commented here.

Note the following formats don't work.
Enabled="<%# Eval("FeatureLink").Equals("Y") %>" -> The server tag is not well formed
If the datasource value is "true" or "false" instead of "Y" or "N",
Enabled='<%# Eval("FeatureLink") %>' -> InvalidCastException
Enabled="<%# Eval("FeatureLink") %>" ->The server tag is not well formed

The C# ternary syntax that doesn't work.
Enabled="<%# Eval("FeatureLink")=="Y" ? "true" : "false" %>" ->The server tag is not well formed
Enabled="<%# Eval("FeatureLink")== null ? "false" : "true" %>" ->The server tag is not well formed
Enabled='<%# Eval("FeatureLink")=="Y" ? "true" : "false" %>' -> Cannot convert type 'string' to 'bool'


2. Eval with string functions / methods
Text='<%# Eval("MoreInfo", "{0}").Replace("\n", "<br/>") %>'
The format "{0}" must be used here. Otherwise Eval("MoreInfo") is an object not a string so it doesn't have the Replace() method. The same applies to other string functions such as ToLower() as mentioned in the first example above.
Text='<%# Eval("MoreInfo").Replace("\n", "<br/>") %> -> the object doesn't have Replace() method...

3. Eval that doesn't use datasource value
NavigateUrl="<%# Request.RawUrl %>"

Edited by user Thursday, 10 March 2016 7:52:08 AM(UTC)  | Reason: Not specified

Sponsor
hong  
#2 Posted : Saturday, 3 March 2018 2:02:22 PM(UTC)
hong

Rank: Administration

Reputation:

Groups: AcademicCoachingSchool, admin, Administration, BookSeller, CatholicSchool, CoachingAdult, CoachingProfessional, CoachingSports, ExtraCurriculumCoaching, IndependentSchool, Moderator, MusicTeacher, PrivateSchool, PublicSchool, SelectiveSchool, tutor
Joined: 23/11/2008(UTC)
Posts: 519

Alternative way to check null and empty:
<asp:Label ID="lblTaxRate" runat="server" Text = '<%# String.IsNullOrEmpty(Convert.ToString(Eval("TaxRate"))) == true ? "" : Eval("TaxRate", "${0}")%>  ' > </asp:Label>

String format
<%# String.Format("{0:C}", Eval("Amount") ) %>

or

<%# ((double)Eval("Amount")).ToString("C") %>
Rss Feed  Atom Feed
Users browsing this topic
Guest
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.