开发者

Adding a control to a list of Controls dynamically

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 fir开发者_运维知识库st control that the data isn't lost when they add a second one and so on.


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.


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 ?


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.


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.


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜