MultiView and View controls are used to create tabs. MultiView controls are containers of a group of View Controls. You can use it to define a group of View Controls. Each View Control contains child controls.
To switch the View, you can use the control ID or View control index value. InMultiViewControl, only oneViewControls are defined as activity views. IfViewThe control is defined as an activity view. Its Child control is displayed on the client. You can use the ActiveViewIndex attribute or the SetActiveView method to define the activity view. IfActiveViewIndexIf the property is nullMultiViewControls do not render any content to the client. If the activity view is setMultiViewTheView, ArgumentOutOfRangeException will be thrown at runtime.
Some common attributes and methods:
ActiveViewIndexAttribute: used to obtain or set the index value of the currently activated View control. The default value is-1, indicating that no View control is activated.
As MultiView and View are relatively simple, let's take a look at the following example.
Create a new ASP. NETWebsite project
1. In the "file" menu, point to "new" and select "website ".
2. In the "New Website" dialog box, select Visual C # From the "language" drop-down list and select an ASP. NET website template.
3. In "location", select HTTP and type the website URL. The default URL is http: // localhost/WebSite. Change to http: // localhost/MultiViewTest and click OK ".
4. Open the Default. aspx designer, switch to the code area, select Ctrl + A, and replace it with the following code:
[C-sharp]View plaincopy
- <% @ Page Language = "C #" AutoEventWireup = "true" CodeFile = "Default. aspx. cs" Inherits = "_ Default" %>
- <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <Html xmlns = "http://www.w3.org/1999/xhtml">
- <Head runat = "server">
- <Title> No title page </title>
- </Head>
- <Body>
- <Form id = "form1" runat = "server">
- <Div>
- <Asp: LinkButton ID = "LinkButton1" runat = "server" onclick = "LinkButton1_Click"> first </asp: LinkButton>
- <Asp: LinkButton ID = "LinkButton2" runat = "server" onclick = "LinkButton2_Click"> second </asp: LinkButton>
- <Asp: LinkButton ID = "LinkButton3" runat = "server" onclick = "LinkButton3_Click"> third </asp: LinkButton>
- <Br/>
- <Hr/>
- <Asp: MultiView ID = "MultiView1" runat = "server" ActiveViewIndex = 1>
- <Asp: View ID = "View1" runat = "server">
- This is the first page
- </Asp: View>
- <Asp: View ID = "View2" runat = "server">
- This is the second page
- </Asp: View>
- <Asp: View ID = "View3" runat = "server">
- This is the third page
- </Asp: View>
- </Asp: MultiView>
- </Div>
- </Form>
- </Body>
- </Html>
Explanation of the above Code:
The MultiView and View Web server controls are used as containers for other controls and tags, and provide a way to conveniently display information to replace the View.
MultiViewControls are used as one or moreViewThe external container of the control.ViewControls can contain any combination of tags and controls.
MultiViewControl displays oneViewControl, and publishViewIn the widget. SetMultiViewThe ActiveViewIndex attribute of the control. You can specify the currently visibleViewControl.
In short, MultiView is a parent container, which includes three View containers. The View container with the index specified by ActiveViewIndex = 1 is displayed, and the other two are hidden. (The index starts from 0 in the order of view)
5. Open Default. aspx. cs, press Ctrl + A to select all, and then replace it with the following code:
[C-sharp]View plaincopy
- Using System;
- Using System. Configuration;
- Using System. Data;
- Using System. Linq;
- Using System. Web;
- Using System. Web. Security;
- Using System. Web. UI;
- Using System. Web. UI. HtmlControls;
- Using System. Web. UI. WebControls;
- Using System. Web. UI. WebControls. WebParts;
- Using System. Xml. Linq;
- Public partial class _ Default: System. Web. UI. Page
- {
- Protected void Page_Load (object sender, EventArgs e)
- {
- }
- Protected void linkbutton#click (object sender, EventArgs e)
- {
- This. MultiView1.ActiveViewIndex = 0;
- }
- Protected void LinkButton2_Click (object sender, EventArgs e)
- {
- This. MultiView1.ActiveViewIndex = 1;
- }
- Protected void LinkButton3_Click (object sender, EventArgs e)
- {
- This. MultiView1.ActiveViewIndex = 2;
- }
- }
6. Save and press Ctrl + F5 to start running. If everything goes well, you can see the following interface:
Click the first, second, and third tabs to switch the content.