사용자가 고객 연락처 정보를 입력해야하는 웹 페이지가 있습니다. 0에서 무한 수의 연락처로 입력 할 수 있습니다. viewstate가이 모두를 저장 처리 할 수 있기 때문에컨트롤 목록에 컨트롤을 동적으로 추가하기
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;
}
이 코드는 작동하지 않습니다 :이 추가 뒤에 내 코드에서
<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"/>
:
나는 페이지의 페이지 코드를 생성 그 제어 데이터. 그러나 이미 추가 된 컨트롤을 저장하는 다른 방법을 생각할 수 없습니다.asp:PlaceHolder
컨트롤의 viewstate는 아무 것도 저장하지 않고 사용자가 일부 컨트롤에 컨트롤을 추가하여 두 번째 컨트롤을 추가 할 때 데이터가 손실되지 않도록 컨트롤을 저장해야합니다. 곧.
Viewstate에 있어야하는 특별한 이유가 있습니까? 대신 Session을 시도 할 수 있습니다. – asawyer
세션은 괜찮지 만 사용자가 페이지를 떠날 경우 세션을 채우지 않을까요? –
세션은 페이지 기반 상태를위한 장소가 아닙니다. 하나는 사용자가 별도의 조치를 취하지 않으면 사용자가 두 페이지를 두 개의 개별 창에서 볼 수 없음을 의미합니다. 대부분의 웹 앱에서 가능해야합니다. –