I have an ASP.NET page with a form that inserts to a database. The page uses a Wizard control since the form is fairl开发者_如何学Cy complex with about 80 form fields and having multiple steps is necessary. It is not databound but the data is collected and inserted from the code behind page. I need to create an identical form for updating records, as this one just inserts. What is the most efficient way to set one up, considering I can reuse code from my first form? What controls should I use? I know how to databind fields for updating when they are in a FormView, but using both a FormView and a Wizard control together seems like it would be complicated. What would work best?
I would have just one Wizard for both, Inserts and Updates. How i handle situations like this is create a Data-Transfer Object or reuse existing Entity that you are working with. Let's say Customer. In the page_load event check if it's a request to Edit (you would probably pass in CustomerID to edit), then load the object from database and store it either in session or in viewstate and set up the formviews (bind them to the object). In the end of the wizard, check, if Customer.ID>0 then
it's an update, else it's an insert. In case it's not an edit and Session["CustomerID"] == null
then Session["Customer"] = new Customer()
and ignore FormView binding, on each step of the wizard update the Customer object in session and in the end perfomr insert. That way you don't have to maintain two almost identical methods.
精彩评论