More Group Sites
Education Books
School Rankings
Jobless Net
Better Home
Enviro++
更好教育论坛
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: 522

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: 522

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") %>
hong  
#3 Posted : Saturday, 15 February 2020 7:35:41 AM(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: 522

Related:

what are these special tags <%# and <%=

<%# is a data binding syntax for use in databound controls.
<%= is a short cut for Response.Write.
<%@ is a directive to include a namespace, page directives, etc.

<% %>  An embedded code block is server code that executes during the page's render phase. The code in the block can execute programming statements and call functions in the current page classhttp://msdn.microsoft.com/en-gb/library/ms178135(vs.80).aspx

<%= %> most useful for displaying single pieces of informationhttp://msdn.microsoft.com/en-us/library/6dwsdcf5(VS.71).aspx

<%# %> Data Binding Expression Syntaxhttp://msdn.microsoft.com/en-us/library/bda9bbfx.aspx

<%$ %> ASP.NET Expressionhttp://msdn.microsoft.com/en-us/library/d5bd1tad.aspx

<%@ %> Directive Syntaxhttp://msdn.microsoft.com/en-us/library/xz702w3e(VS.80).aspx

<%-- --%> Server-Side Commentshttp://msdn.microsoft.com/en-US/library/4acf8afk.aspx

<%: %> Like <%= %> But HtmlEncodes the output (new with Asp.Net 4). http://weblogs.asp.net/scottgu/archive/2010/04/06/new-lt-gt-syntax-for-html-encoding-output-in-asp-net-4-and-asp-net-mvc-2.aspx

 

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.