8

I have a web page where users need to enter customer contact information. They could enter from 0 to an infinite number of contacts.

I created this page code on page:

<ajaxToolkit:ToolkitScriptManager runat="Server" EnablePartialRendering="true" ID="ScriptManager1" />
<asp:PlaceHolder ID="phCustomerContacts" runat="server" EnableViewState="true">/asp:PlaceHolder>
<asp:LinkButton ID="btnAddContact" runat="server" OnClick="btnAddContact_Click" CssClass="LinkButton" Text="Add Contact"/>

In my code behind I added this:

   public void btnAddContact_Click(object sender, EventArgs e)
    {
        IList<CustomerContactProfile> customerContacts = new List<CustomerContactProfile>();
        if (ViewState["CustomerContactList"] != null)
            customerContacts = (List<CustomerContactProfile>)ViewState["CustomerContactList"];
        CustomerContactProfile contactProfile = (CustomerContactProfile)LoadControl("~/Controls/Embedded/CustomerContactProfile.ascx");
        customerContacts.Add(contactProfile);

        foreach (CustomerContactProfile contact in customerContacts)
            phCustomerContacts.Controls.Add(contact);

        ViewState["CustomerContactList"] = customerContacts;
    }

This code doesn't work because the ViewState can't handle storing all of that control data. However, I cannot think of another way to store the controls that were already added.

The viewstate of the asp:PlaceHolder control doesn't save anything and I need the controls to be saved so that if a user puts in some data to the first control that the data isn't lost when they add a second one and so on.

5
  • 1
    Any particular reason it has to be in the Viewstate? You could try Session instead. Commented Feb 7, 2011 at 14:05
  • Session would be fine but if the user leaves the page wouldn't it fill the session up? Commented Feb 7, 2011 at 14:11
  • 1
    Session is absolutely not the place for page-based state. For one, it means that, unless you took extra measures, the user couldn't view two of these pages in two separate windows -- which should be possible in most web apps. Commented Feb 7, 2011 at 16:20
  • What are you doing with these contacts once the user enters them? Will they be stored somewhere, so that when the user pulls up the customer page, they'll still be there (I imagine this is the case)? Commented Feb 7, 2011 at 16:21
  • @Keith - Yes they will be put into the database using Entity Framework Commented Feb 8, 2011 at 12:16

6 Answers 6

1

Rather than store the entire control, simply store the underlying data in session, and rebuild the control set from that data every time you reload the page.

Sign up to request clarification or add additional context in comments.

1 Comment

How would you handle the data in the control being altered? For example, the control has a textbox - if the textbox value is changed and the page reloaded, the changed value would be replaced with the underlying data from the session.
0

I'm not sure it's a best way to add contacts dynamically. Wouldn't it be better to create controls via jquery, and send data for creation to web method ?

1 Comment

jquery is probably better. I have just never used it.
0

It would be better to store the number of controls in viewstate to add instead... And then add them in Page Init or PreInit... ViewState would then be retained for each of the dynamic controls. This would be for postbacks after the button click of course.

HTH.

2 Comments

@Brain - That would work except I would lose all of the information in the form.
No viewstate retains the information changed, as long as you add the controls back in init or preinit.
0

Store the number of controls the user has entered in the view state. Override the LoadViewState page method and add back the number of controls there. The framework will take care of reloading the posted data into the controls for you. You will not lose information. You just have to make sure you add the controls BEFORE the viewstate is restored.

Comments

0

Store it in the Session instead of Viewstate. It's just as bad but it will work!

Comments

0

I think you should not depend on any temporary storage for this -- Viewstate, Session, or otherwise.

You seem to be using your .ascx like I would normally use a class... User control's going to be bigger, though, I imagine, since it has lots of html in it (?).

Anyway, a generic list of a class would be...smaller, at least.

But otherwise, my favorite approach is just to insert each record into a database when it's done (one-by-one) -- at least for manual input, which that's my impression of what you're working with. For example, using a listview, detailsview, gridview, etc.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.