我有一个网页,用户需要输入客户联系信息。他们可以输入从0到无限数量的联系人。
我在页面上创建了这个代码:
<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"/>
在我的代码后台中,我添加了这个:
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;
}
这段代码无法工作,因为ViewState无法处理所有控件数据的存储。然而,我想不到其他存储已添加控件的方法。
asp:PlaceHolder
控件的视图状态不保存任何内容,我需要保存控件,以便如果用户在第一个控件中输入一些数据,当他们添加第二个控件时,数据不会丢失。