How to include other aspx page to main the main aspx page? - asp.net

I am very new to ASP.NET and I am trying to create a website for learning purposes. I was developing the website in PHP where I used to include another PHP file in the main using <?php include('static/nav.php'); ?>. In the same way, I am trying to include another aspx page to the main aspx page for top_nav, side_bar, and footer. But I am not able to do so. Is there any way to include it ? which I am not able to find it.
I have seen somewhere this code
#Include virtual="/menu.aspx" but its not working.

For the main menu bar etc.?
This is normally done without any code or markup on your part.
The concept involves what is called a master form, and then a child form.
So, try creating a new project, and choose ASP.NET frame work).
On the last page, choose web forms - you can un-check https setting
The result is then this:
Notice the menu bar. That is a bootstrap menu bar.
So, now how do we add a web form, say Hello Word?
When you add a web form, you can choose the "master" form that holds that menu bar etc.
So you go like this:
You now get this:
So, our markup is this:
<%# Page Title="" Language="vb" AutoEventWireup="false" MasterPageFile="~/Site.Master"
CodeBehind="helloWord.aspx.vb" Inherits="WebWithMenu.helloWord" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
<h2>Hello Woorld</h2>
</asp:Content>
And thus this is the result:
So you don't' really have to write code - your main "master" page will be part of each new web page you create (as long as you choose new form with master page).
And we can add HelloWord to the menu:
Just open site master, add this:
<li><a runat="server" href="~/Contact">Contact</a></li>
--->this <li><a runat="server" href="~/HelloWord">Hello World</a></li>
Now, we get this:
And if you click on the menu bar, then your HelloWord page will show. So, you can quite much see that we did not have to really write much of anything, and we have that main menu bar that exists for all pages.
this WHOLE process took less time then to write this post!
Now, are there other ways to include content or say another web page?
Well, there is of course iframe - but I think like most, we try and avoid those, but that does let you drop in another page.
There is also what we call "user control"
A User Control is a reusable page or control with an extension of .ascx and created similar to an .aspx page but the difference is that a User Control does not render on its own, it requires an .aspx page to be rendered.
User Controls are very useful to avoid repetition of code for similar requirements. Suppose I need a calendar control in my application with some custom requirements in multiple pages, then instead of creating the control repetitively you can create it once and use it on multiple pages.
Here is a tutorial:
https://www.c-sharpcorner.com/UploadFile/0c1bb2/creating-user-control-in-Asp-Net/
And you can say start to use say jQuery and REALLY nice is jQuery.ui. jQuery.UI is nice since it will let you load contents of a WHOLE page into the current page, and say pop that up as a dialog.
So there are several choices here. But for a top most menu bar etc., I would adopt the master + child design approach.

Related

Add common layout in asp.net

This is not an MVC project.
I already created the layout for the site.
I want to add this layout for every page I create. How to add layout for every page?
I tried with the link below but it doesn't work.
<%# Register src="Layout.aspx" tagname="Layout" tagprefix="abc" Inherits="Layout" %>
An ASPX is not intended to be "included as a layout".
I think you are looking for something named "MasterPage" : see this for example : http://www.w3schools.com/ASPNET/aspnet_masterpages.asp , or https://msdn.microsoft.com/en-us/library/fft2ye18%28v=vs.140%29.aspx and all relative pages.
And if you wish to develop some common component for your pages, look for usercontrols (ASCX).

Master Pages over User Controls

An interview question someone asked me but I was left stumped.
If you can implement user controls inside a normal aspx page, what is the use of a master page then ?
A master page defines a layout template. You put things like styling and scripts in the master page, then call them in any page derived from that master.
It's a way of being able to split presentation out from functionality, and things you need everywhere can be put in the masterpage.

How to assign title to Web Form using Master Page in ASP.NET?

I want to assign an individual title for each page that I add to my solution in VS 2010. I noticed that the Site.Master's title is always displayed on the top of my browser.
Even if I specify a title on a Web Form, I still can't see the title, it shows the one on the Site.Master.
Is there a way that the Site.Master can either inherit the title of a WebForm or simply allow the WebForm to show it's title?
I ran into this a little while ago, and was surprised that it wasn't as straight forward as it seems if you're unfamiliar with it, but my solution was to put the title in the page directive like so:
<%# Page Title="myTitle" Language="vb" ...
in the pages that were being navigated. I don't have title anywhere in my master page either.

Hide content in master page from view page

I have a master page which is shared between about 20 views. In just one view, I need to make one small adjustment to the master page (I need to hide a textbox).
How can I include css or javascript in my view to acheive this?
Or is there some clever trick like including a conditional <% if (View.Name = "blah") { ... } %> that I can stick in my master page?
Thanks for any hints.
As you said you can write a JavaScript code to achieve your goal and include it only in one view. In one of my projects I do the same: I have jQuery AJAX request only at one view, so I just used script tags to include it

master page menus

I want to create a master page for my already developed project.Since the project contains many forms it is quite difficult to include the master page in each form...Is there any possibilities to include the master page in any other simplest way...
Please give some suggestions..
Thanks in advance...
As far as I know, there is no easy way to do this.
You'll have to manually add the masterpage to the page directive
<%# Page MasterPageFile="~/Masterpage.master" ... %>
add the relevant content sections around your pages markup:
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
</asp:Content>
and remove the <html>, <head>, <body> and <form> tags from every page.
Update
Here is an article (and source code) by Bob Powell describing a way to automatically convert html files to aspx files and add a master page. I'm sure you could adapt it to your needs.
This transition is not an easy one to make, as #geoff indicates. It is possible though if you have enough time and patience. The first step is to take all common elements (layout, menu, header, footer, whatever is common) and develop a master page structure. You'll likely need more than 1 depending on the differing layouts of forms in your application. Develop a user control for each of these common sections and make sure the master pages use these controls. Then systematically go through each page of your site and begin implementing the master pages.
As an assistance mechanism, you'll also probably want to have a page baseclass that is capable of communicating through the master page to the contained user controls. In our group we have a standard for setting a property on UserControls and MasterPages called ParentForm that is of type of our primary base page class, and this property is set during the Init of any page or control so that at any time, the developer has access (through Intellisense) to page itself. This is especially helpful since the parent of most controls is a container whose parent is a container whose parent ... you get the idea. For our controls it's just this.ParentForm.
It will be a long process, but MasterPages were really intended to be a "ground up" architectural decision rather than an "employ later" concept.

Resources