This tutorial is for the beginner, who wants to create Tab structure or multi step form in ASP.NET. In this article, we will see how we can create Tab view in ASP.NET. We will use default Multiview control for it.
Thus, let's start.
Multiview will be used to display the content on the tap of Tab.
To show tab on the top, we will use Menu control. Thus, let's go and Add menu control from toolbox. I have given the Horizontal orientation, so that Tab will display horizontally.
- <asp:Menu
- ID="menuTabs"
- Orientation="Horizontal"
- Width="100%"
- runat="server">
- <Items>
- <asp:MenuItem Text="Employee Info" Value="0" Selected="true"/>
- <asp:MenuItem Text="Contact Info" Value="1" />
- <asp:MenuItem Text="Salary Info" Value="2" />
- </Items>
- </asp:Menu>
To display the content for each tab, we will use Multiview control. I have given a property Selected="true" for Employee Info, so that when a page is launched, it will display the content of Employee Info.
- <asp:MultiView ID="multiviewEmployee"
- runat="server" ActiveViewIndex="0">
-
- </asp:MultiView>
Next step is to add view inside Multiview. As I have three menu items, I need to add three views inside Multiview.
- <asp:MultiView ID="multiviewEmployee"
- runat="server" ActiveViewIndex="0">
- <asp:View runat="server">
- <div>
- <%--To do--%>
- </div>
- </asp:View>
- <asp:View runat="server">
- <div>
- <%--To do--%>
- </div>
- </asp:View>
- <asp:View runat="server">
- <div>
- <%--To do--%>
- </div>
- </asp:View>
- </asp:MultiView>
ActiveViewIndex= "0" will display first view after page is launched. Now, we will open corresponding view, when it is clicked on menu items. For it, we need to handle OnMenuItemClick event of Menu control. Hence, add it in your code and it will look, as mentioned below.
- <asp:Menu
- ID="menuTabs"
- Orientation="Horizontal"
- Width="100%"
- runat="server"
- OnMenuItemClick="menuTabs_MenuItemClick">
- <Items>
- <asp:MenuItem Text="Employee Info" Value="0" Selected="true"/>
- <asp:MenuItem Text="Contact Info" Value="1" />
- <asp:MenuItem Text="Salary Info" Value="2" />
- </Items>
- </asp:Menu>
In CS page, assign the value of menu item to multiview .
- protected void menuTabs_MenuItemClick(object sender, MenuEventArgs e)
- {
- Menu menuTabs = sender as Menu;
- MultiView multiTabs = this.FindControl("multiviewEmployee") as MultiView;
- multiTabs.ActiveViewIndex = Int32.Parse(menuTabs.SelectedValue);
-
- }
Now, it's none. Your tab structure is ready. Full code for ASPX is mentioned below.
Hope, you enjoyed the article.