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?

1 Answer 1


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.

  • Very interesting.. I'm always learning something new!
    – Kit Menke
    Commented Jan 5, 2012 at 15:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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