MultiView and View Controls : MultiView « ASP.net Controls « ASP.NET Tutorial






<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>MultiView & View Controls</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <h1>MultiView & View Controls</h1>
     <br/>
       <asp:RadioButtonList AutoPostBack="True" ID="rblView" 
         OnSelectedIndexChanged="rblView_SelectedIndexChanged"
         RepeatDirection="Horizontal" runat="server">
         <asp:ListItem Value="-1">Nothing</asp:ListItem>
         <asp:ListItem Value="0" Selected="True">First</asp:ListItem>
         <asp:ListItem Value="1">Second</asp:ListItem>
         <asp:ListItem Value="2">Third</asp:ListItem>
         <asp:ListItem Value="3">Last</asp:ListItem>
       </asp:RadioButtonList>
       <br />
       Current Index: 
     <asp:Label ID="lblCurrentIndex" runat="server"></asp:Label>
     <br/>
       <asp:MultiView ID="MultiView1" runat="server" 
        ActiveViewIndex="0" 
        OnActiveViewChanged="MultiView1_ActiveViewChanged">
         <asp:View ID="vwFirst" runat="server" 
          OnActivate="ActivateView" 
          OnDeactivate="DeactivateView">
           <h2>
             First View
           </h2>
           <asp:TextBox ID="txtFirstView" runat="server"></asp:TextBox>
           <asp:Button CommandName="NextView" ID="btnNext1" runat="server" Text="Go To Next" />
           <asp:Button CommandArgument="vwLast" CommandName="SwitchViewByID" ID="btnLast" runat="server" Text="Go to Last" />
        </asp:View>
         <asp:View ID="vwSecond" runat="server" 
          OnActivate="ActivateView" 
          OnDeactivate="DeactivateView">
           <h2>
             Second View
          </h2>
           <asp:TextBox ID="txtSecondView" runat="server"></asp:TextBox>
           <asp:Button CommandName="NextView" ID="btnNext2" runat="server" Text="Go To Next" />
           <asp:Button CommandName="PrevView" ID="btnPrevious2" runat="server" Text="Go to Previous" />
         </asp:View>
         <asp:View ID="vwThird" runat="server" 
          OnActivate="ActivateView" 
          OnDeactivate="DeactivateView">
           <h2>
             Third View</h2>
          <br />
           <asp:Button CommandName="NextView" ID="btnNext3" runat="server" Text="Go To Next" />
           <asp:Button CommandName="PrevView" ID="btnPrevious3" runat="server" Text="Go to Previous" />
         </asp:View>
         <asp:View ID="vwLast" runat="server" 
          OnActivate="ActivateView" 
          OnDeactivate="DeactivateView">
           <h2>
             Last View
             </h2>
           <asp:Button CommandName="PrevView" ID="btnPrevious4" runat="server" Text="Go to Previous" />
           <asp:Button CommandArgument="0" CommandName="SwitchViewByIndex" ID="btnFirst" runat="server" Text="Go to First" />
         </asp:View>
       </asp:MultiView>
      <br />
      <br />
      First TextBox:  
      <asp:Label ID="lblFirstTextBox" runat="server" />
      <br />
      Second TextBox:  
      <asp:Label ID="lblSecondTextBox" runat="server" />
      <br />
      <br />
     <strong><span style="text-decoration: underline">
      View Activation History:</span></strong>
     <br />
     <asp:Label ID="lblViewActivation" runat="server" />
      
    
    </div>
    </form>
</body>
</html>

File: Default.aspx.cs

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_PreRender(object sender, EventArgs e)
    {
     lblCurrentIndex.Text = MultiView1.ActiveViewIndex.ToString();
    }

  protected void rblView_SelectedIndexChanged(object sender, EventArgs e)
   {
     MultiView1.ActiveViewIndex = Convert.ToInt32(rblView.SelectedValue);
   }
   
  protected void MultiView1_ActiveViewChanged(object sender, EventArgs e)
   {
     lblFirstTextBox.Text = txtFirstView.Text;
     lblSecondTextBox.Text = txtSecondView.Text;

     rblView.SelectedIndex = MultiView1.ActiveViewIndex + 1;
   }

  protected void ActivateView(object sender, EventArgs e)
   {
     string str = lblViewActivation.Text;
     View v = (View)sender;
     str += "View " + v.ID + " activated <br/>";
     lblViewActivation.Text = str;
   }
   
  protected void DeactivateView(object sender, EventArgs e)
   {
     string str = lblViewActivation.Text;
     View v = (View)sender;
     str += "View " + v.ID + " deactivated <br/>";
     lblViewActivation.Text = str;
   }
 }








3.23.MultiView
3.23.1.MultiView hides and display different areas of a page, useful to create a tabbed page
3.23.2.What is an asp:MultiView (C#)
3.23.3.Displaying a Multi-Part Form
3.23.4.Use the MultiView control to dynamically switch views in a page
3.23.5.MultiView and View Controls
3.23.6.Active index