posts - 218, comments - 222, trackbacks - 68

My Links

News




I am a Microsoft Certified Application Developer MCAD Chartered Member (C# .Net) and born in Bangladesh.
I work for Ocean Informatics Pty Ltd as a Senior Developer - Analyst.
I am also co-founder and core developer of Pageflakes (acquired by LiveUniverse) www.pageflakes.com
and most recently created SmartCodeGenerator

My Articles
Flexible and Plugin based .Net Application..
Mass Emailing Functionality with C#, .NET 2.0, and Microsoft® SQL Server 2005 Service Broker'
Write your own Code Generator or Template Engine in .NET
Smart Code Generator .NET: Usage Overview
Smart Code Generator .NET: Architectural Overview
Smart Code Generator .NET: using with NAnt and Cassini

Archives

Free Programming Language Training

Asp.Net Performance: Use explicit cast instead of using Eval

I have come through lots of examples in the web using DataBinder.Eval but if we go and see whats happening under the hood, we will find Eval uses reflection to evaluate argument passed.

<ItemTemplate>
<div><%# DataBinder.Eval(Container.DataItem,"myfield") %></div>
</ItemTemplate>

But explicit casting can reduce the cost of reflection. Imagine if there is 30 rows and each row has 6 Eval calls. So there will be calls to Eval 180 times. And we should take this pretty seriously.

So you must be thinking what to do. Hey we can simply do something like this and improve significant performance.

Option1
=============
<ItemTemplate>
<div><%# ((DataRowView)Container.DataItem)["myfield"] %></div>
</ItemTemplate>

Option2 for DataReader
=====================
<ItemTemplate>
<div><%# ((DbDataRecord)Container.DataItem).GetString(0) %></div>
</ItemTemplate>

Option3 My favorite use of ItemDataBound Event
==================================================
protected void Repeater_ItemDataBound(Object sender, RepeaterItemEventArgs e)
{
  DataRowView data = (DataRowView)e.Item.DataItem;
  Response.Write(string.Format("<div>{0}</div>",data["myfield"]));
}

And if we are using Collection Objects as DataSource we can do something like this

List<Customer> customers = GetCustomers();
Repeater1.DataSource = customers;
Repeater1.DataBind();

....

protected void Repeater_ItemDataBound(Object sender, RepeaterItemEventArgs e)
{
  Customer customer = (Customer)e.Item.DataItem;
  Response.Write(string.Format("<div>{0}</div>",customer.FirstName));
}

Print | posted on Thursday, November 23, 2006 9:02 PM |

Feedback

Gravatar

# re: Asp.Net Performance: Use explicit cast instead of using Eval

Well, for the 3rd option. I'd have to say it's wrong.
First, you shoulding be writing directly to the response manually. In some cases, you may find this sent to the browser before the page itself!! Also, you shouldn't be writing html as string inside your code, why then do we have markup and code behind/beside ?!
The last time I saw code like that was back in 2004. I had to shout "Hey! Classic ASP Time has passed".
A better solution for this option is to use e.Item.FindControl("ControlId") and cast it to the control type. Of course this doesn't work for non server controls though.
The alternative to this is calling server methods from the markup. You can do databinding to methods and pass them the data item. Something like "#MyMethod((MyDataItemType)Container.DataItem)".
Also remember that if you use the item data bound event you should check for the item type so that you don't do that for header and footer items.
The last thing is that the types you said like DataRowView are applicable when you bind data objects directly to the data bound control.Typically, you won't be doing that, as you'd use business objects instead as part of a 3 tiier architecture.
11/25/2006 7:44 AM | Mohamed Ahmed Meligy
Gravatar

# re: Asp.Net Performance: Use explicit cast instead of using Eval

Sorry by the third option I meant

protected void Repeater_ItemDataBound(Object sender, RepeaterItemEventArgs e)
{
}

doing in the code behind...yes you are right response.write should not be there. (I just made up something there.. realized now it was a bad one.)

Any way my main focus over here is to demonstrate the use of the code behind and the ItemDataBound Event..
11/25/2006 9:00 AM | Shahed Khan
Gravatar

# re: Asp.Net Performance: Use explicit cast instead of using Eval

For more on databinding expressions check this tutorial:
http://www.asp.net/learn/dataaccess/tutorial12cs.aspx?tabid=63
11/25/2006 4:31 PM | Mohamed Ahmed Meligy
Gravatar

# re: Asp.Net Performance: Use explicit cast instead of using Eval

For more on databinding expressions check this tutorial:
http://www.asp.net/learn/dataaccess/tutorial12cs.aspx?tabid=63
11/25/2006 4:32 PM | Mohamed Ahmed Meligy
Gravatar

# re: Asp.Net Performance: Use explicit cast instead of using Eval

For more on databinding expressions check this tutorial:
http://www.asp.net/learn/dataaccess/tutorial12cs.aspx?tabid=63
11/25/2006 4:32 PM | Mohamed Ahmed Meligy
Gravatar

# re: Asp.Net Performance: Use explicit cast instead of using Eval

Sorry for repeating the last comment by mistake. You can delete it.
11/25/2006 4:37 PM | Mohamed Ahmed Meligy
Gravatar

# re: Asp.Net Performance: Use explicit cast instead of using Eval

Sorry for repeating the last comment by mistake. You can delete it.
11/25/2006 4:38 PM | Mohamed Ahmed Meligy
Gravatar

# re: Asp.Net Performance: Use explicit cast instead of using Eval

Finally, Scott Guthrie (the guy that runs the ASP.NET and IIS teams) has covered the topic in many presentations of his own. I think this is one of them: http://weblogs.asp.net/scottgu/archive/2006/08/01/My-ASP.NET-2.0-Tips_2C00_-Tricks_2C00_-Recipes-and-Gotchas-_2200_Highlights-Page_2200_.aspx
11/25/2006 4:39 PM | Mohamed Ahmed Meligy
Gravatar

# re: Asp.Net Performance: Use explicit cast instead of using Eval

Sorry for splitting the comments abd repeating some of them. It seems like there's a probem with submitting commentins in GeeksWithBlogs entirely :S
11/25/2006 4:40 PM | Mohamed Ahmed Meligy
Gravatar

# re: Asp.Net Performance: Use explicit cast instead of using Eval

This is just what I was looking for! I'm binding a list of objects to a GridView, and I needed to be able to invoke a method on the bound object instance inside a data-binding expression. You've shown me how - many thanks! :o)
9/9/2008 8:19 PM | Jonathan Field

Post Comment

Title  
Name  
Email
Url
Comment   
Please add 3 and 6 and type the answer here:

Powered by: