3

我正在根据数据库中的某些条件从代码隐藏创建一个向导控件。我需要从代码中应用布局模板。我检查了 MS 示例,它全部基于设计时,而不是在运行时。

请帮助我设置 asp.net 向导控件的布局模板。最好是一些代码示例。

更新:

我想我应该更详细一点。实际上,我没有看到从模板添加的向导中的控件。这是我的示例代码

public class WizardTemplate:ITemplate
{
    public void InstantiateIn(Control container)
    {
        PlaceHolder header = new PlaceHolder();
        header.Controls.Add(new LiteralControl("I am from Header"));
        header.ID = Wizard.HeaderPlaceholderId;

        PlaceHolder displaySideBar = new PlaceHolder();
        displaySideBar.Controls.Add(new LiteralControl("I am from displaySideBar"));
        displaySideBar.ID = Wizard.SideBarPlaceholderId;

        PlaceHolder wizardStep = new PlaceHolder();
        wizardStep.Controls.Add(new LiteralControl("I am from wizard step"));
        wizardStep.ID = Wizard.WizardStepPlaceholderId;

        PlaceHolder navigation = new PlaceHolder();
        navigation.ID = Wizard.NavigationPlaceholderId;

        container.Controls.Add(header);
        container.Controls.Add(displaySideBar);
        container.Controls.Add(wizardStep);

        container.Controls.Add(navigation);
    }
}

我正在页面初始化上执行此操作。我没有看到我在其中创建的控件。想知道这里出了什么问题。

Wizard testWizard = new Wizard();
        testWizard.LayoutTemplate = new WizardTemplate();
        for (int i = 0; i < 4; i++)
        {
            WizardStep step = new WizardStep();
            step.Title = "Step" + i.ToString();
            step.ID = "Step" + i.ToString();
            step.Controls.Add(new LiteralControl("<b> Step" + i.ToString() + "</b>"));
            testWizard.WizardSteps.Add(step);

        }
        pnlRunTimeWizardContainer.Controls.Add(testWizard);

最终我希望向导使用下面的布局,但在代码中

<asp:Wizard ID="Wizard1" runat="server" EnableViewState="true">
        <LayoutTemplate>
            <div>
                <asp:PlaceHolder ID="headerPlaceHolder" runat="server" />
            </div>
            <div style="float: right">
                <asp:PlaceHolder ID="navigationPlaceHolder" runat="server" />
            </div>
            <div>
                <asp:PlaceHolder ID="sideBarPlaceHolder" runat="server" />
            </div>
            <div>
                <asp:PlaceHolder ID="WizardStepPlaceHolder" runat="server" />
            </div>
        </LayoutTemplate>
        <WizardSteps>
        </WizardSteps>
    </asp:Wizard>
4

1 回答 1

2

干得好:

public class WizardLayoutTemplate : ITemplate
{
  public void InstantiateIn(Control container)
  {
    // do some cool stuff here with the container control
  }
}

myWizard.LayoutTemplate = new WizardLayoutTemplate();
于 2012-05-08T13:34:16.143 回答