Alright, this is driving me crazy. Ive been searching for a solution for several days but it just seems impossible. To see my updated list with the new query I have too click twice before the webpart updates. I heard I should change OnLoad to OnPreRender, but that gives a null reference error on "currentView". What am I doing wrong??

     Label debug;
    Button btn;
    SPWeb curweb;
    TextBox txtfld;
    Microsoft.SharePoint.WebPartPages.XsltListViewWebPart myWebPart;
    SPView currentView;

    Microsoft.SharePoint.WebPartPages.SPLimitedWebPartManager manager;

    protected override void CreateChildControls()
        txtfld = new TextBox();
        btn = new Button();
        debug = new Label();
        btn.Text = "updateview";
        btn.Click += new EventHandler(updateAllProducts_Click);


    protected override void Onload(EventArgs e)
        curweb = SPContext.Current.Web;
        curweb.AllowUnsafeUpdates = true;

        manager = curweb.GetLimitedWebPartManager("/SitePages/Home.aspx", PersonalizationScope.Shared);
        myWebPart = (Microsoft.SharePoint.WebPartPages.XsltListViewWebPart)manager.WebParts.Cast<WebPart>().FirstOrDefault(w => w.Title.Equals("jlist"));
        var list = curweb.Lists["jlist"];
        currentView = list.Views.Cast<SPView>().FirstOrDefault(w => w.ID.ToString("B").Equals(myWebPart.ViewGuid, StringComparison.OrdinalIgnoreCase));

    void updateAllProducts_Click(object sender, EventArgs e)
        currentView.Query = "<Where><Eq><FieldRef Name='Title'/><Value Type='Text'>" + txtfld.Text + "</Value></Eq></Where>";




how about within your createchildcontrols you add an update pannel? that way you can do a partial update, add your controls to the updatepanel.

onprerender wouldnt presist the values on full postback, meaning it would be empty hence why your getting null because the object has not been instsiated at that point in time (not rendered)! whilst your doing this:

  list.Views.Cast<SPView>().FirstOrDefault(w => w.ID.ToString("B").Equals(myWebPart.ViewGuid, StringComparison.OrdinalIgnoreCase));

also like to note that:

OnLoad: This event handles the Load event. This is also used for initialize the control but is not intended for loading data or other processing functionality.

look here for more on that subject:


as for you code within the onload there would be no need, create a method that contains the list query and call it from the createchildcontrols, as the button would be in updatepanel it would be partial update that should refresh the view and update the list ;) but persisting your controls on partial postback. dont forget to use If(!Page.IsPostBack) to add your code within the createchildcontrols

similar thing going on here, hes filtering tho:



As it is your code is highly dependent on the order of events running so that the mutable shared state altered in one event can be observed in another. This is highly complex, difficult to understand, debug, and verify, especially given that the order of events is not consistent between page loads and postbacks.

Rather than getting your information in PageLoad so that you can use it in your click handler, just get the information in your click handler.

Get the SPLimitedWebPartManager object, as well as the SPView object, from inside the click handler if that's where you need them to be.

You can create methods to generate each of those objects, if you end up needing them from the scope of several different locations, just to reduce repetition of code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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