我正在使用DropDownList让用户选择表单模板。然后,用户回发到服务器,并且从模板中的用户控件被动态地呈现给页面。用户填写表单并单击“保存”后,用户控件的ViewState将丢失,值为空。
<air:CreateEditForm ID="CreateEditForm" runat="server" />
<asp:PlaceHolder ID="phDynamicControls" runat="server" />
<p>
<asp:Button ID="btnCreate" runat="server" Text="Create" OnClick="btnCreate_Click" CssClass="action-button" />
<asp:Button ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click" CssClass="action-button" Visible="false" />
<asp:HyperLink ID="linkCancel" runat="server" Text="Cancel" NavigateUrl="Index.aspx" CssClass="action-button" />
</p>
protected void btnCreate_Click(object sender, EventArgs e)
{
CreateDynamicSteps();
}
private void CreateDynamicSteps()
{
foreach (WorkflowTemplate workflowTemplate in template.WorkflowTemplates)
{
foreach (WorkflowTemplateStep workflowStep in workflowTemplate.WorkflowTemplateSteps)
{
if (workflowStep.SubSteps.Count > 0)
{
foreach (WorkflowTemplateStep workflowSubStep in workflowStep.SubSteps)
{
if (workflowSubStep.WhoIsDynamic)
{
dynamicSteps.Add(workflowSubStep);
}
}
}
else if (workflowStep.WhoIsDynamic)
{
dynamicSteps.Add(workflowStep);
}
}
}
// removed for brevity
foreach (WorkflowTemplateStep dynamicStep in dynamicSteps)
{
PersonSelectorDialog personSelector = (PersonSelectorDialog)LoadControl("~/Pages/UserControls/PersonSelectorDialog.ascx");
personSelector.ClientIDMode = System.Web.UI.ClientIDMode.Static;
personSelector.LabelID = "lbl" + dynamicStep.ID;
personSelector.Label = dynamicStep.Name + " Selector";
DynamicPersonSelectorDialogs.Add(personSelector);
}
// removed for brevity
}
private void LoadControls()
{
foreach (PersonSelectorDialog personSelector in DynamicPersonSelectorDialogs)
{
Label title = new Label();
title.ID = personSelector.LabelID;
title.ClientIDMode = System.Web.UI.ClientIDMode.Static;
title.Text = personSelector.Label;
phDynamicControls.Controls.Add(title);
phDynamicControls.Controls.Add(personSelector);
}
}
答案 0 :(得分:-1)
当用户从下拉列表中选择表单模板并触发回发时,需要在某处保留该值,因为当您执行第二次回发时,您需要从第一次回发中再次加载用户控件。