We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Web Part post back problem

Medium Priority
2,038 Views
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?
Comment
Watch Question

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.  

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Jamie McAllisterCloud Expert
CERTIFIED EXPERT
Most Valuable Expert 2013
Top Expert 2014

Commented:
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.
Cloud Expert
CERTIFIED EXPERT
Most Valuable Expert 2013
Top Expert 2014
Commented:
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;
                base.LoadViewState(viewState[0]);
                _GuidanceLinks += (string)viewState[1];
                _PreviousInterrogationLevels = (int)viewState[2];
            }
        }

Open in new window

Author

Commented:
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?
Jamie McAllisterCloud Expert
CERTIFIED EXPERT
Most Valuable Expert 2013
Top Expert 2014

Commented:
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.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.