Multiview and view were the new controls added in ASP.NET 2.0; these controls are very useful for making a tab-like control and may be used to create a simple image gallery with previous and next buttons.
Here are the simple steps to start working with multiview and view controls:
Step 1: You need to just drag and drop the multiview control and the number of views you want inside that multiview. (Note - views can only be taken inside a multiview).
It should appear somewhat like the figure below:
Step 2: Now you need to set the content of each view control; the content can be anything i.e. simple plain text or images or any other ASP or HTML control. In this article we are building an image gallery so we will use images as our content.
Step 3: As from the name multiview it's clear that you can set multiple views to a single control at a time and only one is visible, so now it's time to change controls upon the user's choice. I will do it on a button click; you may do it on whichever event you like. To begin, on the page load event we need to set the first view as a default view; the code of this is written in the page load function as in:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
MultiView1.SetActiveView(View1);
Label1.Text = MultiView1.Views.Count.ToString();
}
}
Here MultiView1 is the name of the multiview taken and SetActiveView method is used to set the view in the active mode i.e which is visible, here its View1. Here the Label1 is used to take a record currently which view is set in the active mode.
Now on button click you need to change the active view which can be simply done by SetActiveView(). I have taken two buttons Prev and Next. The Prev button takes you to the previous image and the next button to the next image in the gallery.
Function on Next Button Click
protected void Next_Click(object sender, EventArgs e)
{
int index = MultiView1.ActiveViewIndex;
int count = Convert.ToInt32(Label1.Text);
if (index == 0)
Prev.Enabled = true;
index = index + 1;
MultiView1.SetActiveView(MultiView1.Views[index]);
if (index+1 == count)
Next.Enabled = false;
}
Function on Prev Button Click
protected void Button1_Click(object sender, EventArgs e)
{
int index = MultiView1.ActiveViewIndex;
int count = Convert.ToInt32(Label1.Text);
if (index+1 == count)
Next.Enabled = true;
index = index – 1;
MultiView1.SetActiveView(MultiView1.Views[index]);
if (index == 0)
Prev.Enabled = false;
}
The above code uses a label which stores the index of the current active view and the index variable stores the number of views present in that multiview:
int index = MultiView1.ActiveViewIndex;
The above line i.e. ActiveViewIndex() gets the number of views present in a multiview.
Hope you enjoy making your image gallery using a simple multiview control rather that using large JavaScripts.
Request for source code if needed or tab control tutorial via multiview.