๐Ÿ“„ ASP.NET โ€“ MultiView & View Controls โ€“ Beginnerโ€™s Guide to Switching UI Sections Dynamically

๐Ÿงฒ Introduction โ€“ What Are ASP.NET MultiView and View Controls?

In ASP.NET Web Forms, the MultiView and View controls allow you to display multiple sections of content on a single pageโ€”one at a time. Think of it like a tab system or step-by-step wizard where only one view is visible at any given moment.

These controls help simplify complex forms or UIs by:

  • Grouping related content in different views
  • Controlling visibility of views using C#
  • Allowing easy navigation between views (like steps)

๐ŸŽฏ In this guide, youโ€™ll learn:

  • What MultiView and View controls are and why theyโ€™re useful
  • How to create multiple views in a form
  • How to switch views using buttons in C#
  • Full working code example with line-by-line explanations and output

๐Ÿ“‚ ASP.NET Web Forms File Structure

๐Ÿ“ File Name๐Ÿ“˜ Role
MultiViewDemo.aspxThe markup file with MultiView, View, and navigation buttons
MultiViewDemo.aspx.csCode-behind file with logic to switch views

๐Ÿ“„ Step 1: Markup File โ€“ MultiViewDemo.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="MultiViewDemo.aspx.cs" Inherits="MultiViewDemo.MultiViewDemo" %>

<html>
<body>
  <form id="form1" runat="server">
    
    <h3>๐Ÿ“„ ASP.NET MultiView Example</h3>

    <!-- Navigation Buttons -->
    <asp:Button ID="btnView1" runat="server" Text="Show View 1" OnClick="btnView1_Click" />
    <asp:Button ID="btnView2" runat="server" Text="Show View 2" OnClick="btnView2_Click" />
    <asp:Button ID="btnView3" runat="server" Text="Show View 3" OnClick="btnView3_Click" />
    <br /><br />

    <!-- MultiView Control -->
    <asp:MultiView ID="multiViewMain" runat="server" ActiveViewIndex="0">

      <!-- View 1 -->
      <asp:View ID="view1" runat="server">
        <h4>This is View 1</h4>
        <asp:Label ID="lblView1" runat="server" Text="Welcome to the first view!" />
      </asp:View>

      <!-- View 2 -->
      <asp:View ID="view2" runat="server">
        <h4>This is View 2</h4>
        <asp:Label ID="lblView2" runat="server" Text="This is the second view." />
      </asp:View>

      <!-- View 3 -->
      <asp:View ID="view3" runat="server">
        <h4>This is View 3</h4>
        <asp:Label ID="lblView3" runat="server" Text="Now you're seeing view three." />
      </asp:View>

    </asp:MultiView>

  </form>
</body>
</html>

๐Ÿ” Explanation of Controls

Control/Attribute๐Ÿ“˜ What It Does
<asp:MultiView>Main container that holds multiple <View> sections
ActiveViewIndex="0"Sets which view to show by default (0 = View1)
<asp:View>Represents a single section or page of content
Navigation ButtonsUsed to switch between views using C# logic

โš™๏ธ Step 2: Code-Behind File โ€“ MultiViewDemo.aspx.cs

public partial class MultiViewDemo : System.Web.UI.Page
{
    protected void btnView1_Click(object sender, EventArgs e)
    {
        // Show View 1
        multiViewMain.ActiveViewIndex = 0;
    }

    protected void btnView2_Click(object sender, EventArgs e)
    {
        // Show View 2
        multiViewMain.ActiveViewIndex = 1;
    }

    protected void btnView3_Click(object sender, EventArgs e)
    {
        // Show View 3
        multiViewMain.ActiveViewIndex = 2;
    }
}

๐Ÿ” Code Explanation for Beginners

Code/Line๐Ÿ’ก What It Means
multiViewMain.ActiveViewIndexChanges which View is currently shown (0, 1, or 2)
btnView1_Click, etc.These methods are triggered when their respective buttons are clicked
ActiveViewIndex = 1Will make the second view (View2) visible

๐Ÿ–ฅ๏ธ Output Preview in Browser

โ–ถ๏ธ On First Load:

[Show View 1] [Show View 2] [Show View 3]
โ†“
This is View 1
Welcome to the first view!

โ–ถ๏ธ After Clicking “Show View 2”:

[Show View 1] [Show View 2] [Show View 3]
โ†“
This is View 2
This is the second view.

โ–ถ๏ธ After Clicking “Show View 3”:

[Show View 1] [Show View 2] [Show View 3]
โ†“
This is View 3
Now you're seeing view three.

โœ… Only one view is displayed at a time. The others are hidden based on the ActiveViewIndex.


๐Ÿ“Œ Summary โ€“ Recap & Takeaways

ASP.NET MultiView and View controls are perfect for organizing pages with:

  • Multiple steps (e.g., registration wizard)
  • Tab-like sections (e.g., profile, settings, billing)
  • Dynamic content areas that change based on user action

๐Ÿ” Key Learnings:

  • MultiView holds several View sections
  • You control which view is shown using ActiveViewIndex
  • Buttons can switch between views using simple C# logic
  • View content is preserved between switches (thanks to ViewState)

โœ… Use MultiView when you want to reduce page reloads and organize related content in one place.


โ“ Frequently Asked Questions (FAQs)


โ“ Can I have more than 3 Views inside a MultiView?
โœ… Yes. You can have as many Views as you wantโ€”just update the ActiveViewIndex to switch.


โ“ Whatโ€™s the difference between Panel and View?
โœ… A Panel is always visible (unless you hide it), but Views inside a MultiView work exclusivelyโ€”only one can be visible at a time.


โ“ Does each View remember its contents when hidden?
โœ… Yes. ASP.NET maintains ViewState for all Views inside the MultiView unless disabled.


โ“ Can I put other controls (TextBox, Button) inside each View?
โœ… Absolutely! Each View can contain any valid ASP.NET control.


Share Now :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

๐Ÿ“„ ASP.NET โ€“ Multi Views

Or Copy Link

CONTENTS
Scroll to Top