Monday, March 31, 2008

Boosting performance on aspx pages

Most of internet tutorials tells you to use Page Load event to bind your data to controls. What's happening when you have to rebind controls again because your data source is changed due to some event where you have to re bind controls.
Well, the answer is: use OnPrender event for binding your control.
Let's presume we have the following situation: we have a page where we have a list with a lot of items(say for example products) and a button where we add products


<asp:TextBox ID="txtProduct" runat="server"></asp:TextBox>
<asp:Button ID="btnAdd" runat="server" Text="Add product" Width="57px"
onclick="btnAdd_Click" />

<br />
<asp:ListBox ID="lstProducts" runat="server"></asp:ListBox>
The usual way(without optimization) of doing yhis would be like this:
1. We use session to keep products list read from the database as optimization(not to read products on page load each time)


List<string> Products
{
get
{
if (Session["Products"] != null)
{
return (List<string>)Session["Products"];
}
return null;
}
set
{
Session["Products"] = value;
}
}
Here comes the page load event (our first binding)



protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//here we read products form database or whereever we need
//I'll just pun some dummy products in here
Products = new List<string>();
for (int i = 0; i < 20; i++)
{
Products.Add("Product " + i.ToString());
}
}
//Binding No 1
//here we bind products to the list
lstProducts.DataSource = Products;
lstProducts.DataBind();
Now suppose we click Add product, let's see what happens:
1. Page do a page load ad do the binding once.
2. Page execute click event, add the new product to the list then do a rebind of the list of products
In conclusion we have 2 bindings.



protected void btnAdd_Click(object sender, EventArgs e)
{
//here we add the new product form textbox on Add event
Products.Add(this.txtProduct.Text);
//our product list is changed so we have to rebind list control
//Binding No 2
lstProducts.DataSource = Products;
lstProducts.DataBind();
}
Well, imagine you have a page with a lot of controls, and some of you controls events must force you to rebind a lot. Imagine how this can affect page loading performance.

Now, let's see the optimized way of doing some data binding.
1.First we see our new page load method without data bind. Here we only read products first time a page is loaded.


protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//here we read products form database or whereever we need
//I'll just pun some dummy products in here
Products = new List<string>();
for (int i = 0; i < 20; i++)
{
Products.Add("Product " + i.ToString());
}
}
}
You can see no databinding here.

2.Second we change the button click


protected void btnAdd_Click(object sender, EventArgs e)
{
//Here we only add product to the list
//here we add the new product form textbox on Add event
Products.Add(this.txtProduct.Text);
}


3. Third and last thing is to implement binding in OnPrerender event:


protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
//Here is the only time when we do the binding
lstProducts.DataSource = Products;
lstProducts.DataBind();
}
Well, you can see binding is done only once in page. This is only a demonstrative case, but imagine your page have a lot of controls to bind, what happens then?
kick it on DotNetKicks.com

No comments: