ASP.NET Wizard控件,如何动态添加一个步骤?

时间:2010-10-21 17:09:50

标签: c# asp.net c#-4.0 wizard

代码隐藏:

public partial class WebForm1 : System.Web.UI.Page
{
    protected override void OnInit(EventArgs e)
    {
        var t = new TemplatedWizardStep { Title = "Lalalal" };
        t.Controls.Add(new Step1UserControl());
        _WizardWebControl.WizardSteps.Add(t);
        base.OnInit(e);
    }
}

页面标记:

<asp:Wizard runat="server" id="_WizardWebControl">

Step1UserControl.ascx标记:

<fieldset>
    <legend>General Informations</legend>
     <p>TEST DYNAMIC</p>    
</fieldset>

步骤在左侧栏显示标题,但HTML(字段集和段落)不显示在步骤中。它也需要是TemplatedWizardStep,因为我们使用Template作为布局。如何动态添加步骤?

1 个答案:

答案 0 :(得分:1)

我不确定这种方式是最佳做法,但它确实有效:步骤1 UserControl应该实现Template接口,

public void InstantiateIn(Control container)
{
    container.Controls.Add(this);
}

然后onInit可能如下所示:

protected override void OnInit(EventArgs e)
{
    TemplatedWizardStep templatedWizardStep =  new TemplatedWizardStep { Title = "Lalalal" };

    //  load control by path to initialize markup
    ITemplate control = (ITemplate)Page.LoadControl("\\Step1UserControl.ascx");                        

    templatedWizardStep.ContentTemplate = control;            
    wizard.WizardSteps.Add(templatedWizardStep);

    //  make it visible
    wizard.MoveTo(templatedWizardStep);
    base.OnInit(e);   
}