Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

MultiView and Wizard: A Powerful Duo



ASP.NET 2.0 introduces two new related controls to create a group of interchangeable panels of child controls. The MultiView control defines a group of views, each represented with an instance of the View class. Only one view is active at a time and rendered to the client. The View control can’t be used as a standalone component and can only be placed inside a MultiView control. Here’s an example.

<asp:MultiView runat="server" id="Tables">
   <asp:View runat="server" id="Employees">
       :
   </asp:View>
   <asp:View runat="server" id="Products">
       :
   </asp:View>
   <asp:View runat="server" id="Customers">
       :
   </asp:View>
</asp:MultiView>

You change the active view through postback events when the user clicks button or links embedded in the current view. To indicate the new view, you can either set the ActiveViewIndex property on the MultiView control or pass the view object to the SetActiveView method. For example, imagine a page that contains a drop-down list control named Views:

<asp:DropDownList Runat="server" ID="Views" AutoPostBack="true">
   <asp:ListItem Value="Employees" /> 
   <asp:ListItem Value="Products" /> 
   <asp:ListItem Value="Customers" /> 
</asp:DropDownList>

The drop-down list posts back whenever the user changes the selected item. During the Page_Load event, you select the view to display:

void Page_Load(object sender, EventArgs e)
{
   // Views is an auto-postback drop-down list   
   Tables.ActiveViewIndex = Views.SelectedIndex;
}

The combination of View and MultiView controls lends itself very well to implementing wizards. In fact, the new ASP.NET Wizard control uses a MultiView control internally.

The Wizard control is a composite control that uses the MultiView control internally to display and hide the panels that form the steps of the wizard. Here’s an example of the wizard:

<asp:wizard runat="server" id="BookWizard"
    style="border:solid 1px" width="300" height="100"
    onfinishbuttonclick="Finished">
<WizardSteps>
    <asp:WizardStep steptype="Start">
         <h3>Thanks for shopping with us.
         Please, proceed with payment!</h3>
    </asp:WizardStep>
    <asp:WizardStep steptype="Step">
         <h3>Enter your credit card number:</h3>
         <asp:textbox runat="server" id="CreditCard" text="" />
    </asp:WizardStep>
   <asp:WizardStep steptype="Finish">
        <h3>You're all set. Click and your credit card
        will be charged. Thank you!</h3>
    </asp:WizardStep>
    <asp:WizardStep steptype="Complete">
        <asp:label runat="server" id="FinalMsg" />    
    </asp:WizardStep>
</WizardSteps>
</asp:wizard>

The control provides navigation buttons and fires server-side events whenever the user clicks to change the page. The navigation can be both linear and nonlinear meaning that you can jump from one page to the next as well as randomly to any listed page. When the Finish button is clicked, you typically collect all the data and proceed with the final step. Because all the controls are part of the page, you can access them codewise using their ID. The following code represents the final step of the wizard that finalizes the whole operation:

void Finished(object sender, EventArgs e)
{
    // Perform the operation
    :
 
    // Give feedback
    string msg = "Credit card <b>[{0}]</b> charged successfully.";
    FinalMsg.Text = String.Format(msg, CreditCard.Text);
}

Wizard and MultiView controls together make implementing multistep operations a snap in ASP.NET 2.0.


Dino Esposito is Wintellect's ADO.NET and XML expert, and a trainer and consultant based in Rome, Italy. Dino is a contributing editor to Windows Developer Network and MSDN Magazine, and the author of several books for Microsoft Press including Building Web Solutions with ASP.NET and ADO.NET and Applied XML Programming for .NET. Contact Dino at [email protected].



Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.