Reputation: 256741
Microsoft's introduction to data-binding using the asp:Repeater
control gives the syntax to fetch a value:
<b><%# DataBinder.Eval(Container.DataItem, "orderid") %></b>
This syntax is repeated on other introductions to the ASP.net Repeater
control:
<a href="<%# DataBinder.Eval(Container.DataItem, "URL") %>">...</a>
But i remember this syntax being "bad" and "wrong". From the MSDN documentation of DataBinder.Eval
i see:
Note
Because this method performs late-bound evaluation, using reflection at run time, it can cause performance to noticeably slow compared to standard ASP.NET data-binding syntax.
(emphases added)
So that explains why i had a memory of "Eval
is bad". But what is the "standard ASP.NET data-binding syntax"?
Upvotes: 5
Views: 2878
Reputation: 124716
This MSDN page describes standard databinding syntax as follows:
In the following code fragment, for example, an integer is displayed as a currency string. With the standard ASP.NET data-binding syntax, you must first cast the type of the data row in order to retrieve the data field, IntegerValue. Next, this is passed as an argument to the String.Format method:
<%# String.Format("{0:c}", ((DataRowView)Container.DataItem)["IntegerValue"]) %>
Upvotes: 0
Reputation: 907
Well "standard ASP.NET data-binding" generally refers to binding data from a data source to named controls, such as the textboxes on a form, or the old GridView where you can bind an entire table.
If you just need a read only display of data, you might consider a foreach loop or a DataReader to parse the data into raw HTML in a stringBuilder and then dump the StringBuilder into a Literal.
Upvotes: 1
Reputation: 7326
I think you can just do a cast to avoid the "late binding" penalty:
<%# (((System.Data.DataRowView)Container.DataItem)["URL"]) %>
Now let's say you increase performance by x%. Is it worth it to your application? The trade-off (in my opinion) is less readability.
Upvotes: 3