Tuesday, February 9, 2010

Grid CausedCallback and IsCallback values are incorrect

I am using a ComponentArt grid on one of the ASP.Net pages. In the Page_Load() I have the code to load the grid. The code looks something like this...


protected void Page_Load(object sender, System.EventArgs e)
{
  if(Page.IsPostBack == false)
  {
    buildGrid();
  }
}

This works well, the buildGrid() is called only when the page is loaded for the first time and when a postback occurs the function is skiiped.

However my grid a paging enabled and grid runs at server side (runat="server"). This mean grid would postback everytime you naviage from page to page. So adding another check as follows...


protected void Page_Load(object sender, System.EventArgs e)
{
  if(Page.IsPostBack == false &&
   Grid1.CausedCallabck ==false )
  {
    buildGrid();
   }
}


This means buildGrid() is skipped when when Grid1 caused the callback (e.g. navigating from page to page within the grid).

But here is the problem. The Grid1.CausedCallback is false when you load the page for the first time and changes to true when you change pages on the grid but remain true there after. Even if you navigate away from the page that contains the grid to something totally different and come back to this page Grid1.CausedCallback remains true. This can cause many problem based on the implementation you have.

I am not sure what the problem is, it could be a bug in the component itself. I did spend sometime to figure it out but with no luck. So I decided to use a work around.

Work around:

The Request object has a property called UrlReferrer. This basically tells us from which page the request is coming to the current page. This mean when ever the UrlReferrer is otherthan current page(page with the grid) we know the user is landing on current page for the first time. So the final code looks something like this.


protected void Page_Load(object sender, System.EventArgs e)
{
  string url = Request.UrlReferrer.ToString();
  if(Page.IsPostBack == false &&
    url.ToLower().Contains("currentpagename.aspx")==false )
  {
   buildGrid();
  }
}