Update Panel trigger with GridView and ObjectDataSource design-time binding

Last post 07-24-2008 2:44 PM by hpdotnet. 6 replies.

Sort Posts:

  • Update Panel trigger with GridView and ObjectDataSource design-time binding

    07-23-2008, 2:29 PM
    • Loading...
    • hpdotnet
    • Joined on 09-29-2006, 10:57 AM
    • Posts 38

    Hi,

    I am testing a simple scenario with a GridView bound to a ObjectDataSource at desgin time - by using the DataSourceId attribute of GridView.

    As soon as the page loads, it automatically triggers the Select method of the ObjectDataSource. Being a long query, it takes a while for the page to load.  On other pages of the app, I have a button that triggers the Update Panel postback. I show a progress bar in the async request.

    I do not want an unnecessary button click event to trigger the UpdatePanel async postback as there is no need of a button on this page.

     And I do not want to manually bind the data source to the GridView.

     How do I trigger the UpdatePanel async postback on the initial page load? Any ideas will be greatly appreciated.

    Here's my code:

     

           <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Always" >
                <ContentTemplate>
                    <div id="divGridView">
                        <asp:GridView  ID="GridView1" SkinID="epwGridView" runat="server" DataSourceID="odsLocation">
                            <Columns>
                                <asp:BoundField HeaderText="Location"  DataField="LOCATION_CODE" SortExpression="LOCATION_CODE" ItemStyle-Width="100"/>
                                <asp:BoundField HeaderText="Description" DataField="DESCRIPTION" SortExpression="DESCRIPTION" ItemStyle-Width="200" ReadOnly="true" />
                            </Columns>
                        </asp:GridView>
                    </div>
                </ContentTemplate>
                <Triggers>
    
                </Triggers>
            </asp:UpdatePanel>
        </div>    
        <asp:ObjectDataSource ID="odsLocation" runat="server"  
            SelectMethod="GetLocations" 
            TypeName="EPWWEB.BLL.Maintenance.LocationBL" >
        </asp:ObjectDataSource>
      

     Thanks

    hpdotnet 

     

  • Re: Update Panel trigger with GridView and ObjectDataSource design-time binding

    07-23-2008, 3:12 PM

    If you change your UpdatePanel's UpdateMode to Conditional and then in your Page_Load call the UpdatePanel1.Update() if its not a postback.

    We're all in this together.
  • Re: Update Panel trigger with GridView and ObjectDataSource design-time binding

    07-23-2008, 3:37 PM
    • Loading...
    • hpdotnet
    • Joined on 09-29-2006, 10:57 AM
    • Posts 38

    Doesn't work. It does not trigger the async postback.

    I tried putting the ObjectDataSource inside the UpdatePanel too. Didn't help.

    I must be missing something simple. I just need the GridView to be populated asynchoronusly on the initial page load. 

    I have seen this on so many ajax-enabled pages. Do they all use a dummy button click/postback event ?

     Thanks

    hpdotnet 

     

  • Re: Update Panel trigger with GridView and ObjectDataSource design-time binding

    07-23-2008, 3:54 PM
    • Loading...
    • aadreja
    • Joined on 02-03-2007, 11:36 AM
    • Ahmedabad, India
    • Posts 351

    try using AJAX timer control like this.

    //set your updatepanel mode to "conditional"
    <asp:GridView  ID="GridView1" SkinID="epwGridView" runat="server">
                            <Columns>
                                <asp:BoundField HeaderText="Location"  DataField="LOCATION_CODE" SortExpression="LOCATION_CODE" ItemStyle-Width="100"/>

    <asp:BoundField HeaderText="Description" DataField="DESCRIPTION" SortExpression="DESCRIPTION" ItemStyle-Width="200" ReadOnly="true" />
                            </Columns>
    </asp:GridView>

    //don't set DataSourceID="odsLocation" initially
    //place a timer control

    <asp:Timer runat="server" id="UpdateTimer" interval="1000" ontick="Timer1_Tick" />
    // interval in miliseconds


    //on timer1 tick
    protected void Timer1_Tick(object sender, EventArgs e)
    {
          GridView1.DataSourceId =
    "odsLocation";
          GridView1.DataBind();
          UpdatePanel1.Update();
          Timer1.Enabled=false; // disable timer as we don't want to repeat this task
    }

    hope this helps, let me know in case of any problems.

    Thanks,
    Ritesh

    ---------------------------------------------
    Please do not forget to mark as Answer if my reply is helpful.
  • Re: Update Panel trigger with GridView and ObjectDataSource design-time binding

    07-23-2008, 4:31 PM
    • Loading...
    • hpdotnet
    • Joined on 09-29-2006, 10:57 AM
    • Posts 38

     Hi aadreja,

    Thanks for your reply. I cannot use that option because I need to specify the DataSourceId in the GridView at design time. This GridView is going to use the ObjectDataSource for editing, paging etc. Manually binding it is creating complications in more places.

     Do you know in what Page/Control event does the databinding occur.

    I am thinking on these lines -

    Check if the databinding event is async or not. If not, then cancel it and issue an async request, by calling UpdatePanel.Update(); . Will that work? Or any other ideas?

    Thanks

    hpdotnet 

     

     

  • Re: Update Panel trigger with GridView and ObjectDataSource design-time binding

    07-23-2008, 5:19 PM
    Answer
    • Loading...
    • aadreja
    • Joined on 02-03-2007, 11:36 AM
    • Ahmedabad, India
    • Posts 351

    Ok its simple.

    Don't change DataSourceId

    write this code OnSelecting of object datasource

    protected void odsLocation_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
    {
        
    if (!IsPostBack)
         {
             e.Cancel =
    true; //cancel selecting when page is loaded first time
       
    }
    }

    protected void Timer1_Tick(object sender, EventArgs e)
    {
          GridView1.DataBind();
          UpdatePanel1.Update();
          Timer1.Enabled=false; // disable timer as we don't want to repeat this task
    }

    //in aspx

    <asp:ObjectDataSource ID="odsLocation" runat="server" SelectMethod="GetLocations"
      
    TypeName="EPWWEB.BLL.Maintenance.LocationBL" OnSelecting="odsLocation_Selecting">
    </asp:ObjectDataSource>

     

    let me know how it goes.

    Thanks,
    Ritesh

    ---------------------------------------------
    Please do not forget to mark as Answer if my reply is helpful.
  • Re: Update Panel trigger with GridView and ObjectDataSource design-time binding

    07-24-2008, 2:44 PM
    • Loading...
    • hpdotnet
    • Joined on 09-29-2006, 10:57 AM
    • Posts 38

     Hi Ritesh,

    Thanks for your idea. It works. I still have to play with the timer interval setting  but the concept works.

    The main trick was to cancel the ODS binding in its OnSelecting event. I am debating between using an invisible button and timer. I feel Timers can throw up surprises.

     Here's the code:

     

           <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
                <ContentTemplate>
                    <div id="divGridView">
                        <epw:EpwGridView  ID="GridView1" SkinID="epwGridView" runat="server" DataSourceID="odsLocation" EpwSessionId="LocationBL" EpwEnableEditing="true" >
                            <Columns>
                                <asp:BoundField HeaderText="Description" DataField="DESCRIPTION" SortExpression="DESCRIPTION" ItemStyle-Width="200" />
                            </Columns>
                        </epw:EpwGridView>
                    </div>
                    <asp:Timer runat="server" id="Timer1" Interval="3000" Enabled="false" OnTick="Timer1_Tick"/>       
                </ContentTemplate>
            </asp:UpdatePanel>
            <asp:ObjectDataSource ID="odsLocation" runat="server"  
                SelectMethod="GetLocations" UpdateMethod="UpdateLocation" OnSelecting="odsLocation_Selecting"
                TypeName="EPWWEB.BLL.Maintenance.LocationBL" >
                <UpdateParameters>
                    <asp:Parameter Name="DESCRIPTION" Type="String" />
                </UpdateParameters>
            </asp:ObjectDataSource> 
    

      

      

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                Timer1.Enabled = true;
            }
        }
     
        protected void Timer1_Tick(object sender, EventArgs e)
        {
            if (Page.IsPostBack)
            {
                GridView1.DataBind();
            }
            Timer1.Enabled = false; // disable timer as we don't want to repeat this task
        }
    
        protected void odsLocation_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
        {
            if (!IsPostBack)
            {
                e.Cancel = true; //cancel selecting when page is loaded first time
                GridView1.EmptyDataText = "";
            }       
        }
      
Page 1 of 1 (7 items)
Microsoft Communities
Page view counter