Add a control to the list of controls dynamically

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.

+8
source share
6 answers

Instead of storing the entire control, simply save the underlying data in the session and reinstall the control set from that data each time you reload the page.

+1
source share

I'm not sure if this is the best way to dynamically add contacts. Isn't it better to create controls via jquery and send the data to be created in a web method?

0
source share

It would be better to save the number of controls in the viewstate to add instead ... And then add them to the Page Init or PreInit ... The ViewState will then be saved for each of the dynamic controls. That would be for postback after clicking a button, of course.

NTN.

0
source share

Save the number of controls that the user has entered in the view state. Override the LoadViewState page method and add the number of controls there. This structure will take care of reloading the posted data into the controls for you. You will not lose information. You just need to make sure that you add controls before the view state is restored.

0
source share

Store it in a session instead of Viewstate. It's just as bad, but it will work!

0
source share

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

It seems you are using your .ascx, as usual, I would use a class ... There will be more user control, though, I think, since it has a lot of html (?).

In any case, the general list of the class will be ... less, at least.

But otherwise, my favorite approach is only to insert each record into the database when it was made (one at a time) - at least for manual input, which is also my impression of what you are working with. For example, using listview, detailsview, gridview, etc.

0
source share

All Articles