I have a web page where users need to enter customer contact information. They can enter from 0 to an infinite number of contacts.
I created this page code on the 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 does not work because ViewState cannot process all control data. However, I cannot come up with another way to store controls that have already been added.
The asp:PlaceHolder control's presentation element does not save anything, and I need the controls to be saved so that if the user places some data in the first control, the data is not lost when the second is added, etc.
Ben hoffman
source share