I have a multi-user set of ASP.NET web pages. The pages use AJAX update panels, so I can avoid updating the screen with every postback. The life cycle of each page is as follows:
1. During Page_Load, get relevant data for the user from the web service.
2. Save the data (rather large) and the service link in a static dataset.
3. Allow various changes to data parts using screen controls (grids, text fields)
4. Verification of data received through the form
5. send updated data back to the service
I do this using static variables in the page class itself as follows:
public partial class MyPage : System.Web.UI.Page { static xxxx.DataCaptureServiceClient m_Service; //reference to web service static string m_PersonID = string.Empty; //current person_id page is viewing static ServResponse m_ServiceResult = null; // reference to our data to edit ( ServResponse is a large data contract) static string m_SortExpression = "Reference"; //default sort expression for grid const int PERSONID_COLUMN = 0; //column index in grid for the personID column const int STATUS_COLUMN = 4; //column index in grid for the application status protected void Page_Load(object sender, EventArgs e) { try { if (!Page.IsPostBack) { //get new service instance m_Service = new xxxx.DataCaptureServiceClient(); ShowDataOnPage(); //get data in m_ServiceResult and bind to a grid on screen } } catch (Exception ex) { Response.Redirect("ErrorPage.aspx", false); } } protected void butNext_Click(object sender, EventArgs e) { try { Page.Validate(); if (Page.IsValid) { //use m_ServiceResult and m_Service to send a packaged submission to the service SendDatatoService(); Response.Redirect("TheNextPage.aspx", false); } } catch (Exception ex) { Response.Redirect("ErrorPage.aspx", false); } } //Other methods which allow edits to m_ServiceResult
I am wondering if:
A) Is this a good way to implement or are there best practices?
B) Should I clear the memory by setting all statics to NULL when I redirect to another page?
C) If I delete statics, do I risk losing data from another user?
UPDATE
I rewrote removing statics, storing const values โโand passing me the data as parameters. Where I need to store data for updates, I saved the minimum amount that I need in the session [] variables.
source share