views:

27

answers:

1

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?

A: 

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

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);   
}
ika
@abatishchev: Please do not change class name to var, using vars when it is unnessesay is a bad practice
ika