2012-05-08 91 views
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); 
    } 
} 

我在頁面init上這樣做。我沒有看到我在其中創建的控件。想知道這裏出了什麼問題。

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> 

回答

2

在這裏你去:

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

myWizard.LayoutTemplate = new WizardLayoutTemplate(); 
相關問題