Ben*_*man 8 .net c# asp.net .net-4.0
我有一个网页,用户需要输入客户联系信息.他们可以从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"/>
Run Code Online (Sandbox Code Playgroud)
在我的代码后面我添加了这个:
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;
}
Run Code Online (Sandbox Code Playgroud)
此代码不起作用,因为ViewState无法处理存储所有控制数据.但是,我想不出另一种存储已添加控件的方法.
asp:PlaceHolder控件的视图状态不保存任何内容,我需要保存控件,以便在用户向第一个控件输入一些数据时,数据在添加第二个数据时不会丢失,依此类推.