Search code examples
c#asp.netc#-4.0wizard

ASP.NET Wizard control, how to add a step dynamically?


Code-behind:

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);
    }
}

Page markup:

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

Step1UserControl.ascx markup:

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

The step show at the left bar with the Title, but the HTML (fieldset and the paragraph) is not displayed in the step. It requires to be a TemplatedWizardStep too because we use Template for the layout. How do I add a Step dynamically?


Solution

  • I'm not sure that this way is the best practice, but it works:

    Step1UserControl should implement ITemplate interface,

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

    and then onInit may look like this:

    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);   
    }