0

我正在使用 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);
  }
}
4

1 回答 1

-1

当用户从下拉列表中选择表单模板并触发回发时,该值需要保留在某处,因为当您执行第二次回发时,您需要从第一次回发中再次加载用户控件。

于 2012-04-23T12:09:22.473 回答