Flash
Flash

Reputation: 16703

Prevent UpdatePanel loading entire page on server

As I understand, when an UpdatePanel is updated, the entire page is rebuilt but only content inside the UpdatePanel is actually reloaded on the page.

Some parts of my page are quite slow to render (due to database calls) and I don't want all these to reload on every postback if it only needs to reload a small part of the page.

Example - At the top of the page (outside the UpdatePanel) I display a set of totals, and inside the UpdatePanel I have a grid with 'Next Page' buttons. When I click 'next', I want the grid to update but I don't want the server to query the database for all the totals again.

What's the best way to do this?

Upvotes: 2

Views: 1194

Answers (3)

Erik Dekker
Erik Dekker

Reputation: 2433

You can check if the postback is from the updatepanel. If thats the case then you just don't calculate the totals.

If you have multiple updatepanels then you can check what updatepanel did the postback by checking this:

bool isUpdatePanelPostBack 
        = ScriptManager.GetCurrent(Page).AsyncPostBackSourceElementID
          .Equals(yourUpdatePanel.UniqueID);

if(!isUpdatePanelPostBack) {
    //calculate your totals 
}

If you have only one updatepanel on your page then you can just check the IsInAsyncPostback for setting the bool:

bool isUpdatePanelPostback = ScriptManager.GetCurrent(Page).IsInAsyncPostBack;

Upvotes: 1

graham mendick
graham mendick

Reputation: 1839

The standard approach would be to have a !PostBack check before running the totals logic. That way they're only calculated the first time the page loads and after that they're held in the ViewState of the control you're using to display them.

Alternatively, if you use databinding (with ObjectDataSource) then this would be handled for you automatically since the databinding would only happen the first time the page loads.

Upvotes: 0

Phaedrus
Phaedrus

Reputation: 8421

You can always check the IsAsync property of the page to exclude code blocks from running.

Upvotes: 0

Related Questions