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
The usual way(without optimization) of doing yhis would be like this:
<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>
1. We use session to keep products list read from the database as optimization(not to read products on page load each time)
Here comes the page load event (our first binding)
List<string> Products
{
get
{
if (Session["Products"] != null)
{
return (List<string>)Session["Products"];
}
return null;
}
set
{
Session["Products"] = value;
}
}
Now suppose we click Add product, let's see what happens:
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();
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.
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.
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();
}
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.
You can see no databinding here.
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());
}
}
}
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:
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?
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
//Here is the only time when we do the binding
lstProducts.DataSource = Products;
lstProducts.DataBind();
}
No comments:
Post a Comment