MultiView Example, How to use MultiView Control in ASP.NET 2.0/3.5/4.0, MultiView <asp:MultiView /> Control

The MultiView and View controls are new addition to ASP.NET 2.0. If you have ever created a Tab Control in ASP.NET, you will find MultiView and View control very familiar.

A View control works as a container of common controls and a MutliView control works as a container of several View controls. In this step by step tutorial, I will show you how to create a MultiView control and generate different views depending on the selection.

<asp:MultiView ID="MultiViewData" runat="server" ActiveViewIndex="0">
<asp:View ID="ViewTab1" runat="server">
This is ViewTab1. Click the button to goto View 2.
</asp:View>
<asp:View ID="ViewTab2" runat="server">
This is ViewTab2. Enter your name and click the button to goto View 3.<br />
Name: <asp:TextBox ID="fldName" runat="server" />
</asp:View>
<asp:View ID="ViewTab3" runat="server">
<asp:Literal ID="litName" runat="server" /> This is ViewTab3.
</asp:View>
</asp:MultiView>


<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" />

<asp:UpdatePanel ID="UP1" runat="server">
<ContentTemplate>

<asp:MultiView ID="MultiViewData" runat="server" ActiveViewIndex="0">
<asp:View ID="ViewTab1" runat="server">
This is ViewTab1. Click the button to goto View 2.
</asp:View>
<asp:View ID="ViewTab2" runat="server">
This is ViewTab2. Enter your name and click the button to goto View 3.<br />
Name: <asp:TextBox ID="fldName" runat="server" />
</asp:View>
<asp:View ID="ViewTab3" runat="server">
<asp:Literal ID="litName" runat="server" /> This is ViewTab3.
</asp:View>
</asp:MultiView>
<br /><br />
<asp:Button ID="butSubmit" runat="server" Text="Continue" onclick="butSubmit_Click" />

</ContentTemplate>
</asp:UpdatePanel>
</form>


protected void butSubmit_Click(object sender, EventArgs e)
{
if (MultiViewData.ActiveViewIndex == 0)
{
MultiViewData.SetActiveView(ViewTab2);
}
else if (MultiViewData.ActiveViewIndex == 1)
{
MultiViewData.SetActiveView(ViewTab3);
if (String.IsNullOrEmpty(fldName.Text))
{
litName.Text = "You did not enter your name. ";
}
else
{
litName.Text = "Hello, " + fldName.Text + ". ";
}
}
else if (MultiViewData.ActiveViewIndex == 2)
{
MultiViewData.SetActiveView(ViewTab1);
}
}

MultiView and View controls together provide a wizard-like functionality in ASP.NET pages. The MultiView control is a container control that can house only View Controls. The View Controls, which are also container controls, can only be housed in MultiView controls; any attempt to place them on a web page shows up as a design time error. Hence they will be used together with the MultiView control which may contain a number of View controls. Within the View control, one may place any of the other standard controls. That one may even place a MultiView control in a View Control opens the way for providing a complex navigational functionality in web pages.

MultiView Example:

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
    protected void Page_Load(object sender, System.EventArgs e) {
        if(!Page.IsPostBack){
            MultiTabView1.ActiveViewIndex = 0;
           }
    }
    void NextImage(object sender, System.EventArgs e)
    {
        MultiTabView1.ActiveViewIndex += 1;
    }
    protected void Page_PreRender(object sender, System.EventArgs e) {
        LabelDisplayTxt.Text = "Beautiful Animal Image: " + (MultiTabView1.ActiveViewIndex + 1).ToString() + " of " + MultiTabView1.Views.Count.ToString();
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>MultiView Control With C# Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="LabelDisplayTxt" runat="server" Font-Size="Large" ForeColor="Crimson"></asp:Label>
        <br /><br />
        <asp:MultiView ID="MultiTabView1" runat="server">
            <asp:View ID="View1" runat="server">
                <asp:Image ID="ImageA" runat="server" ImageUrl="~/Images/Animal1.jpg" />
                <br />
                <asp:Button ID="ButtonA" runat="server" Text="Next Image" OnClick="NextImage" />
            </asp:View>
            <asp:View ID="View2" runat="server">
                <asp:Image ID="ImageB" runat="server" ImageUrl="~/Images/Animal2.jpg" />
                <br />
                <asp:Button ID="ButtonB" runat="server" Text="Next Image" OnClick="NextImage" />
            </asp:View>
            <asp:View ID="View3" runat="server">
                <asp:Image ID="ImageC" runat="server" ImageUrl="~/Images/Animal3.jpg" />
                <br />
                <asp:Button ID="ButtonC" runat="server" Text="Next Image" OnClick="NextImage" />
            </asp:View>
            <asp:View ID="View4" runat="server">
                <asp:Image ID="ImageD" runat="server" ImageUrl="~/Images/Animal4.jpg" />
                <br />
                <asp:Button ID="ButtonD" runat="server" Text="Next Image" OnClick="NextImage" />
            </asp:View>
            <asp:View ID="View5" runat="server">
                <asp:Image ID="ImageE" runat="server" ImageUrl="~/Images/Animal5.jpg" />
            </asp:View>
        </asp:MultiView>
   
    </div>
    </form>
</body>
</html>
Tags: , ,
Hot on Web:


About author