Today we will create a navigation stucture, using JavaScript, C and #.NET.
While developing Web parts, I got one new requirement to create navigation/tab structure, using C#.
The steps are given below to create it.
- Open Visual Studio -> create an empty project -> add Visual Web part.
- Open ->ascx file. In this file, we need to add div as follows. (it may vary according to the project requirement).
- <div id="divmain">
- <div id="divNavigation">
- <div id="divtab1" onclick="hideshow(this.id);" class="navigation"> Home </div>
- <div id="divtab2" onclick="hideshow(this.id);" class="navigation">HR </div>
- <div id="divtab3" onclick="hideshow(this.id);" class="navigation">IT </div>
- <div id="divtab4" onclick="hideshow(this.id);" class="navigation">Contact US </div>
- <div id="divtab5" onclick="hideshow(this.id);" class="navigation">Questions </div>
- </div>
- <div id="divContent">
- <div id="HomeContent">
- <div> Add ur Content for home page </div>
- </div>
- <div id="HRContent" style="display:none">
- <div> Add ur Content for HR page </div>
- </div>
- <div id="ITContent" style="display:none">
- <div> Add ur Content for IT page </div>
- </div>
- <div id="ContactContent" style="display:none">
- <div> Add ur Content for Contact page </div>
- </div>
- <div id="QuestionsContent" style="display:none">
- <div> Add ur Content for Questions page </div>
- </div>
- </div>
- </div>
- Add CSS given below to get the look and feel of tab structure.
- <style>
- .navigation {
- width: 80px;
- height: 10px;
- background: burlywood;
- padding: 14px;
- margin-bottom: 20px;
- border: 1px solid white;
- font-weight: bold;
- cursor: pointer;
- }
-
- div#divNavigation {
- display: flex;
- }
- </style>
- Add JavaScript given below to hide show div based on the clicked tab.
- <script type="text/javascript">
- function hideshow(elmt) {
- if (elmt == "divtab1") {
- $("#HomeContent").show();
- $("#HRContent").hide();
- $("#ITContent").hide();
- $("#ContactContent").hide();
- $("#QuestionsContent").hide();
- } else if (elmt == "divtab2") {
- $("#HomeContent").hide();
- $("#HRContent").show();
- $("#ITContent").hide();
- $("#ContactContent").hide();
- $("#QuestionsContent").hide();
- } else if (elmt == "divtab3") {
- $("#HomeContent").hide();
- $("#HRContent").hide();
- $("#ITContent").show();
- $("#ContactContent").hide();
- $("#QuestionsContent").hide();
- } else if (elmt == "divtab4") {
- $("#HomeContent").hide();
- $("#HRContent").hide();
- $("#ITContent").hide();
- $("#ContactContent").show();
- $("#QuestionsContent").hide();
- } else if (elmt == "divtab5") {
- $("#HomeContent").hide();
- $("#HRContent").hide();
- $("#ITContent").hide();
- $("#ContactContent").hide();
- $("#QuestionsContent").show();
- }
- }
- </script>
The script is very easy. You need to change the div contents and its data according to your requirement.
- Build and deploy the project.
- Create one Web part page and in Web part given above, you will see the tab structure, as shown below.