Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
670
InitializeDataSource event causes double the work...
posted

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_InitializeDataSource
UltraWebGrid_InitializeLayout
FormView1_Load
UltraWebToolbar_ButtonClicked (next button to change the page of the formview)
FormView1_PageIndexChanged
UltraWebGrid_InitializeDataSource
Label_DataBinding (pass the value of this control to the data source )
UltraWebGrid_InitializeLayout
FormView1_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...

Parents
  • 80
    posted

    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_Load
    UltraWebGrid_InitializeDataSource
    UltraWebGrid_InitializeLayout

    On a Click of a search button, I get:

    Page_Init. Page.IsPostBack? True ScriptManager.IsInAsyncPostBack? True
    Page_Load
    UltraWebGrid_InitializeDataSource
    UltraWebGrid_InitializeLayout
    SearchBtn_Click
    UltraWebGrid_InitializeLayout

    On a SelectedRowsChange event I get:

    Page_Init. Page.IsPostBack? True ScriptManager.IsInAsyncPostBack? True
    UltraWebGrid_InitializeDataSource
    UltraWebGrid_InitializeLayout
    Page_Load
    UltraWebGrid_SelectedRowsChange

    And on a Sort event I get:

    Page_Init. Page.IsPostBack? True ScriptManager.IsInAsyncPostBack? True
    UltraWebGrid_InitializeDataSource
    UltraWebGrid_InitializeLayout
    Page_Load
    UltraWebGrid_SortColumn
    UltraWebGrid_InitializeLayout

    And on a PageIndexChange event I get:

    Page_Init. Page.IsPostBack? True ScriptManager.IsInAsyncPostBack? True
    UltraWebGrid_InitializeDataSource
    UltraWebGrid_InitializeLayout
    UltraWebGrid_InitializeLayout
    Page_Load
    UltraWebGrid_PageIndexChange
    UltraWebGrid_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_InitializeDataSource
    UltraWebGrid_InitializeLayout
    UltraWebGrid_InitializeLayout
    Page_Load
    UltraWebGrid_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.

    It reminds me that I had a "stupid" thing before:
    UltraWebGrid1.Clear();
    UltraWebGrid1.DataSource = sl.List;
    UltraWebGrid1.DataBind();

    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

Reply Children
No Data