I realize this is not a normal case but I am running a web grid on a MS FormView where the data in the webgrid (LoadOnDemand=XML) is dependent on a bound control for the current page in the form. This means that as the user moves from record to record, the sequence of events are:
UltraWebGrid_InitializeDataSourceUltraWebGrid_InitializeLayoutFormView1_LoadUltraWebToolbar_ButtonClicked (next button to change the page of the formview)FormView1_PageIndexChangedUltraWebGrid_InitializeDataSourceLabel_DataBinding (pass the value of this control to the data source )UltraWebGrid_InitializeLayoutFormView1_DataBound
In simple terms, when the user presses the next button to go to the next page, the page posts back, initializes the grid with the old value, pages forward, then re-initializes the grid with the new page value. I cannot detect that the postback is due to a page change because the buttonclicked event happens long after the grid events have fired.
Of course if I was not depedent on a bound control for the value then I would not have to call the databind method and trigger a repeat of the initializedatasource and initializelayout events.
Any thoughts on how to simplify the process and eliminate the duplication of effort?
I will keep trying things and post a new approach if I figure it out so maybe someone else could benefit...
I similarly wanted to know why my InitializeLayout event was called multiple times. My ultrawebgrid in inside an aspnet UpdatePanel (that seems irrelevant here).
A fresh load of a page gives me:
Page_Init. Page.IsPostBack? False ScriptManager.IsInAsyncPostBack? False Page_LoadUltraWebGrid_InitializeDataSourceUltraWebGrid_InitializeLayout
On a Click of a search button, I get:
Page_Init. Page.IsPostBack? True ScriptManager.IsInAsyncPostBack? TruePage_LoadUltraWebGrid_InitializeDataSourceUltraWebGrid_InitializeLayoutSearchBtn_ClickUltraWebGrid_InitializeLayout
On a SelectedRowsChange event I get:
Page_Init. Page.IsPostBack? True ScriptManager.IsInAsyncPostBack? TrueUltraWebGrid_InitializeDataSourceUltraWebGrid_InitializeLayoutPage_LoadUltraWebGrid_SelectedRowsChange
And on a Sort event I get:
Page_Init. Page.IsPostBack? True ScriptManager.IsInAsyncPostBack? True UltraWebGrid_InitializeDataSourceUltraWebGrid_InitializeLayoutPage_LoadUltraWebGrid_SortColumnUltraWebGrid_InitializeLayout
And on a PageIndexChange event I get:
Page_Init. Page.IsPostBack? True ScriptManager.IsInAsyncPostBack? True UltraWebGrid_InitializeDataSourceUltraWebGrid_InitializeLayoutUltraWebGrid_InitializeLayoutPage_LoadUltraWebGrid_PageIndexChangeUltraWebGrid_InitializeLayout
I'm doing a UltraWebGrid1.DataBind(); in both PageIndexChanged and SortColumn events. Whilst writing this post, I realise that calling DataBind() in the SortColumn event or pageIndexChanged event is the thing that trigger an extra InitializeLayout. Removing it means I don't get the duplicate, and the grid still seems to work.
My Search Click button calls DataBind() because I'm changing my dataset/datatable content. I can see that pageIndex and Sort don't change the datasource.
Now after removing all my DataBind() apart from my SearchCLick event, I still get 2 InitializeLayout when I do a Sort event:
Page_Init. Page.IsPostBack? True ScriptManager.IsInAsyncPostBack? True UltraWebGrid_InitializeDataSourceUltraWebGrid_InitializeLayoutUltraWebGrid_InitializeLayoutPage_LoadUltraWebGrid_SortColumn
And once again, as I write this, I realise why... I was calling this in my code:UltraWebGrid1.Bands[0].SortedColumns.Clear();and after removing it, I only get a single InitializeLayout.
and the Clear() is not necesssary and it was causing a double InitializeLayout too (1 for clear, and 1 databind).
My question now is: Do we need DataBind() in the grid events? What does it achieve?
Regards,T
It seems that whenever you alter the structure of the grid (e.g. clear the sorted columns) the grid needs to re-evaluate everything related to its layout, hence calling InitializeLayout several times if needed.
As for the DataBinding question, I found this blog post by lead technical evangelist Tony Lombardo extremely useful:
http://blogs.infragistics.com/blogs/tony_lombardo/archive/2008/01/29/demystifying-infragistics-webgrid-databinding.aspx
The relevant quote from the blog post relating to your question is (but the whole post is good as well, so going through it will help a lot in understanding how UltraWebGrid databinding works):
"Because of the strict requirements the grid has on when it needs data, the InitializeDataSource event is how the Grid will request data. In this event, you must assign the grid's datasource (and be sure if it's a dataset, it has data in it). You don't need to call DataBind, just setting the datasource is enough in this event. "
You can also checkout the latest slide on the advanced grid databinding presentation from the Devscovery conference here:
http://blogs.infragistics.com/blogs/tony_lombardo/archive/2008/04/03/devscovery-slides.aspx