I have a ASP.Net create user wizard. One of the additional steps I included is to gather contact information for connections the customer may have to fellow co-workers so the account being configured can batch create logins for them as well. The user I use for ContactPerson consist of several text boxes for key info (Name, DoB, Company Name, Address and State).
I use a simple button to invoke the AddContacts method which creates the proper nesting structure used for RadPanelItems. As soon as I add the first item it works fine, the second one though wipes out the first via postback and lack of data persistence.
This user control maps to an EF4 Entity, specifically the page has a member called List ContactList. I wanted to ask what would be the best way for me to persist my Entity data I gathered from the previous contact along with re-adding my user control (with said data) back to the form when a post back is encountered?
I am open to suggestions and I am looking for a best practice solution(s). Thanks in advance.
Until you persist you data, you can use the HttpContext.Current.Session object to store this info. The Session (stored on the server) is better for larger data, the ViewState (stored in a hidden field on the page) can be used for smaller data as it is sent back and forth every request and quickly blows up the page size. For discussion see ViewState Vs Session ... maintaining object through page lifecycle or http://msdn.microsoft.com/en-us/library/ms972976.aspx
Annother option (not a good one for your situation) is QueryParameters for very small data like IDs or primary Keys having the advantage that you can copy paste the url to an email and still have the info. see http://www.codeproject.com/Articles/5876/Passing-variables-between-pages-using-QueryString
So back to your issue: For every new contact you add, perform something like this until you finally persist your data.
var contacts = new List<EF4Entity>()
if (Session["Contacts"] != null){
contacts = Session["Contacts"] as List<EF4Entity>
}
var newContact = new EF4Entity()
//fill your new contact here
contacts.Add(newContact)
//bind your RadPanelItems here
//store it to the session
Session["Contacts"] = contacts
You will need to use an intermediate storage location unless you persist and retrieve your data to the database with every request (which I find inflexible for business applications of any complexity).
Here's how I approach it.
I have written a wrapper around ASP.Net Session to ensure that I have unique object keys and that I can enforce the maximum number of objects in Session per user. Each of my pages binds to an object which implements a simple interface (I call it IModel). This way, I can deal with the model for every page/view consistently, and I can make any page state-aware.
Dynamic controls are appended to the model for the page directly or to a descendant. This methodology supports simple or complex object structures with ease and also allows the sharing of data between pages.