Friday, July 24, 2009

DatabindExpressions

Digging Into Data Binding Expressions

The case of the "Eval" should be maintained. ie. "E" should be CAPS and remaining should be small.
<%#Eval("ProductName")%>,<%#FormatPrice(Eval("UnitPrice"))%>

'<%#DataBinder.Eval(Container.DataItem,"ID")%>'

'<%# Eval("ID")%>'

'<%# Eval("HotelAmenities").ToString().Replace("±","") %>'

'<%# Eval("PropertyID", "Javascript:window.open(\"Map.aspx?PID={0}\")") %>'

Call a function from the aspx page when binding using "Eval"


ImageUrl ='<%# LoadImage(Convert.ToInt32(((System.Data.DataRowView)Container.DataItem)["StarRate"]),3) %>'

LoadImage => Function Name with two parameters starrate and imagenumber

Convert.ToInt32(((System.Data.DataRowView)Container.DataItem)["StarRate"]) => Convert "StarRate" to int


Concat String with "Eval"

Text='<%# "Customer Review: "+ ((System.Data.DataRowView)Container.DataItem)["CustomerRatings"].ToString()+"/5" %>'



PostBackUrl='<%# "~/HotelDetails.aspx?PropertyID=" + Eval("PropertyID") %> '

OR
PostBackUrl="'<%# "~/HotelDetails.aspx?PID=" + Eval("PropertyID") %> '"


PostBackUrl="~/SysAdmin/modifyAnnouncement.aspx?id=<%# Eval("intAnnouncementID")%>"
PostBackUrl="~/HotelDetails.aspx?PropertyID=<%# Eval("PropertyID")%>"

PostBackUrl="~/HotelDetails.aspx?PropertyID=" + '<%# Eval("ID")%>'

PostBackUrl='<%# Eval("PropertyID", "~/HotelDetails.aspx?PropertyID={0}") %>'


DataBinder.Eval versus Explicit Casting

http://unboxedsolutions.com/sean/archive/2005/03/06/455.aspx

http://weblogs.asp.net/rajbk/archive/2004/07/20/what-s-the-deal-with-databinder-eval-and-container-dataitem.aspx


As a regular DataBinder.Eval() user, now I feel a little
bit guilty after reading this KB article, and I'm curious
how many ASP.NET developers use explicit casting on a regular basis.
Drop me a line and let me know.

Let's say you are binding to a DataSet. This...

<%# ((System.Data.DataRowView)Container.DataItem)["au_id"] %>

...is significantly faster than this...

<%# DataBinder.Eval(Container.DataItem, "au_id") %>


Conditions with Eval


<%#(DataBinder.Eval(Container.DataItem, "checkedout" ) == "No"), ?"Check Out":""%>


'<%# !String.IsNullOrEmpty(Convert.ToString(((System.Data.DataRowView)Container.DataItem)["RackRate"] ).Trim()) ? "£ "+((System.Data.DataRowView)Container.DataItem)["RackRate"]:"" %>'


PopUpS
------

OnClientClick='<%# Eval("PropertyID", "Javascript:popupWin(\"Map.aspx?PID={0}\",500,400)") %>'


OnClientClick='<%# Eval("PropertyID", "Javascript:window.open(\"Map.aspx?PID={0}\")") %>'

OR

onClientClick='<%#String.Format("{0}\"{1}{2}{3}{4}\")", "javascript:window.open(","Map.aspx?PID=",Eval("PropertyID"), "&Guid=",Eval("GUID")) %>'

OnClientClick="popup(' <%# ((System.Data.DataRowView)Container.DataItem)["PropertyID"] %>');"

OnClientClick='<%# Eval("PropertyID", "Javascript:window.open(\"Map.aspx?PropertyID={0}\")") %>'


OnClientClick='<%# Eval('PropertyID', "Javascript:window.open('Map.aspx?PropertyID={0}','top=200,left=350,width=425,height=350, menubar=no,status=no,location=yes,toolbar=no,scrollbars=no')") %>'


"window.open(<%# DataBinder.Eval(Container.DataItem,"PropertyID","'Map.aspx?PID={0}'") %>,'question','scrollbars=yes,width=630,height=480,location=no,menubar=no,status=no,toolbar=no');"

No comments: