我有一个网页,用户需要输入客户联系信息。他们可以从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
控件的视图状态不保存任何内容,我需要保存控件,以便如果用户将一些数据放入第一个控件中,以便在添加第二个控件时数据不会丢失并且等等。
任何特殊的原因,它必须在视图状态?您可以尝试使用会话。 – asawyer 2011-02-07 14:05:50
会话会很好,但如果用户离开页面不会填满会话吗? – 2011-02-07 14:11:09
会话绝对不是基于页面状态的地方。首先,这意味着,除非采取额外措施,否则用户无法在两个单独的窗口中查看其中的两个页面 - 这在大多数Web应用程序中都应该可以实现。 – 2011-02-07 16:20:00