Web Part post back problem

Posted on 2009-02-10
Last Modified: 2013-11-07
I have a web part that I am trying to use within Sharepoint 2007. The web part has two drop down menus that are used to execute a SQL stored procedure.  When the stored procedure is executed it creates a table with the rows being dynamically determined by the stored procedure results.  I also have a button within the web part that adds a new row to the table, this causes a post back and a new row is added.  The problem comes when the post back occurs as the code to dynamically create the table is called again and any information the user has changed on the page reverts back to the data from the stored procedure.

Is it possible to have data in a table edited and then keep that same information when a post back occurs?
Question by:bobstery
    LVL 9

    Assisted Solution

    Yup, and in pretty much the same way as you might when creating a server control in c#.  

    You could use viewstate, a session variable (provided sessions are turned on), a cookie even.

    You might even run some code to see if the table exists already, but that approach is  a bit silly.  

    LVL 31

    Expert Comment

    by:Jamie McAllister MVP
    Sounds like you need to store the data in Viewstate and not make the call to DB on every page load.

    Look into overriding the SaveViewState and LoadViewState methods to handle your data between postbacks.
    LVL 31

    Accepted Solution

    Here's a real example of using Viewstate in a webpart. You can save a variety of data types.

    As Neil says you could also use Session, the trade off between Session and Viewstate is that sesison eats up Server memory, and Viewstate increases the amount of info passed in the request and response. (However Sharepoint pages are heavy already so a little extra viewstate usually makes no difference).

    Don't use cookies for this.
    protected override object SaveViewState()
                object[] viewState = new object[3];
                viewState[0] = base.SaveViewState();
                viewState[1] = _GuidanceLinks;
                viewState[2] = _InterrogationLevels;
                return viewState;
            protected override void LoadViewState(object savedState)
                object[] viewState = null;
                if (savedState != null)
                    viewState = (object[])savedState;
                    _GuidanceLinks += (string)viewState[1];
                    _PreviousInterrogationLevels = (int)viewState[2];

    Open in new window


    Author Comment

    So if I save the contents of the table into an array (in the SaveViewState method) I can load the data back into the web part in the LoadViewState method?
    LVL 31

    Expert Comment

    by:Jamie McAllister MVP
    You can put a one dimensional array in viewstate. Any more dimensions than that are tricky.

    If you can't use the array for this reason you could serialise it to XML and store that away in a string or some such.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that undeā€¦
    These days socially coordinated efforts have turned into a critical requirement for enterprises.
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    This video discusses moving either the default database or any database to a new volume.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now