Saturday, 7 June 2008

ASP.NET Wizard - How do I modify templated buttons at runtime?

Accessing a Wizard's templated buttons at runtime is quite tricky and not very elegant using this approach - any better ideas/solutions are more than welcome!

<asp:Wizard ID="MyWizard" runat="server">
<StartNavigationTemplate>
<asp:Button ID="btnNext" runat="server" CommandName="MoveNext" Text="Next" />
</StartNavigationTemplate>
</asp:Wizard>

Control startNavigationTemplate = MyWizard.FindControl( "StartNavigationTemplateContainerID" ) as Control;

if ( startNavigationTemplate != null )
{
Button nextButton = startNavigationTemplate.FindControl( "btnNext" ) as Button ;

if ( nextButton != null )
{
// Do something.
}
}

To manipulate buttons in the step and finish navigiation templates, use StepNavigationTemplateContainerID and FinishNavigationTemplateContainerID respectively.

3 comments:

Anonymous said...

Hey thanks this is much more concise than the other solutions I have found for this problem. Any ideas what the Id of the HeaderTemplate is? I want to modify some content in the HeaderTemplate.

Andrew Gunn said...

You can use the following code to get control of the HeaderTemplate:

Control header = EcommerceWizard.FindControl( "HeaderContainer" ) as Control;

Piotr Benetkiewicz said...

I have just saved my day! Thank you!