SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a webpart where I access a sharepoint list and render contents of that list.

At the moment I'm fetching the data in CreateChildControls().

When a button is clicked the data will be fetched again (filtered by text in a form input field). This happens in the HandleClickEvents() method.

Because of the lifecycle, CreateChildControls() will always be called before HandleClickEvents(), so the data will be fetched twice.

Is there a way to avoid that or respectively, where is the right place to fetch the data?

share|improve this question
up vote 6 down vote accepted

Usually in Page_Load or in CreateChildControls you should place the check for IsPostback and load the data only if the page is not in the postback mode. And also, you should load the data in all postback handlers.

This way, the data is loaded always and only once per page load.

But while this solution will work perfectly for a separated page, things get much more complicated if we are talking about webparts. The point is, that anything on the page could cause the postback, not only this particular webpart.

So, you probably will need to get the control which has caused the postback, and add additional check to ensure, that it is your control.

For instance, the code might look something like this:

public override void CreateChildControls()
    // ...
    var targetControlId = Page.Request.Params.Get("__EVENTTARGET");
    if (!Page.IsPostback || !myControlIds.Contains(targetControlId))
    // ...

, there myControlIds contains collection of IDs of controls in your webpart, which can cause postback.

share|improve this answer
Very interesting.. I'm always learning something new! – Kit Menke Jan 5 '12 at 15:18

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.