Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2015
  • Last Modified:

Web Part post back problem

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?
0
bobstery
Asked:
bobstery
  • 3
2 Solutions
 
neilrichardsCommented:
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.  

0
 
Jamie McAllister MVPArchitectCommented:
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.
0
 
Jamie McAllister MVPArchitectCommented:
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

0
 
bobsteryAuthor 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?
0
 
Jamie McAllister MVPArchitectCommented:
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.
0

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now