PowerPages – High Performance Guidelines: Data binding

How Can We Help?

PowerPages – High Performance Guidelines: Data binding

< Back

Since Power Pages provide fine-grained control over PostBacks, data binding and caching, they can be leveraged to implement high performance pages.
The following article provides some guidelines and recommendations on how to achieve this.

This article addresses the topic of

  • Data binding

See the links below for articles on other high performance topics:

Data binding

Data binding is the process of loading data from a data source (e.g. Database or API) and populating controls with the loaded data.

Avoid unnecessary data binding

Since data binding is a very expensive operation to run, it is essential for high performance pages that no unnecessary data binding is happening.

ASP.Net Web Forms (.aspx pages) generally require that controls be populated with data on every load of the page (including PostBacks and Callbacks), however, if the logic executing on a PostBack or Callback doesn’t require the data, then we need to avoid this unnecessary data work.

Checking for PostBacks and Callbacks

Make use of conditional checks to prevent data binding operations happening during PostBacks and Callbacks if the control doesn’t need to be interrogated or updated.

It is a common inefficiency to indiscriminantly data bind a control on the Page_Load event, without checking if the page is in a PostBack or Callback. It is important to remember that the Page_Load does execute for every PostBack and Callback, and it is necessary to check before data binding.

 

Automatic binding when using SqlDataSource control

If you are using an SqlDataSource control to perform your data binding, it will automatically execute when the page loads. This can be confusing and difficult to control, so it is recommended that you handle the data binding event on the control you are populating where you will finally be able to decide if and how the control should be populated.

[C#] protected void grid_DataBinding(object sender, EventArgs e) { bool loadData = false; //Determine if grid data should be loaded, setting loadData = true if(loadData) { grid.DataSource = SqlDataSource1.Select(DataSourceSelectArguments.Empty); } }

Another way to prevent the SqlDataSource from executing automatically is to leave the `DataSourceID` property on the control to be data bound blank initially, and then assign it to the ID of the SqlDataSource only when data binding should take place.

A data binding strategy

The following data binding strategy aims to reduce unnecessary data binding. It assumes that you are aiming to reduce PostBacks and using Callbacks to update the page.

1. Handle the DataBinding event

Handling the DataBinding event allows you to control when and how the data is assigned to the control. In this event you can manually assign the DataSource property of the control.

2. Add conditional logic to your Page_Load event

The following conditional statements help to separate logic that must run on the first page load and then on subsequent callbacks.

[C#] protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) //First load of page only { grid.DataBind(); } else if (Page.IsCallback) { if(grid.IsCallback) grid.DataBind(); //data bind on grid callbacks (paging, sorting, filtering etc.) } }

3. Explicitly call DataBind() method

Explicitly call the DataBind() method during PostBacks where data needs to be reloaded.
The below example shows this happening after some changes are saved.

[C#] protected void btnSaveChanges_Click(object sender, EventArgs e) { //Save changes to database grid.DataBind(); //reload latest data }

Caching data

Data sets that might be large, slow to retrieve, or frequently used and infrequently changed, are good candidates for caching. This way the retrieved data can be stored in memory on the server and doesn’t need to be retrieved from the source every time it is needed.

SqlDataSource caching

If using an SqlDataSource control for data binding, caching can be enabled (along with a cache duration) and the control will automatically cache the result of the Select statement.

[C#] mySqlDataSource.EnableCaching = true; mySqlDataSource.CacheDuration = 900; //15 minutes x 60 sec/min = 900 sec

Page.Cache object

Data from other sources can also be cached by storing them in the Page.Cache object.

See the PowerPages – High Performance Pages – Caching article for more recommendations on caching.

 

Comments are closed.

This is the legacy version of the XMPro Documentation site. For the latest XMPro documentation, please visit documentation.xmpro.com

X