Add common layout in asp.net - 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).

Related

How to include other aspx page to main the main aspx page?

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.

What's the best way of structuring hiearchy of views in ASP.NET MVC?

I'm currently converting a ASP.NET webforms solution to MVC.
In webforms we besides the site.master we have master pages where we keep the common code and head imports for each section. So a normal page inheritance goes like so:
Site.master -> section.Master -> page.aspx
In MVC I'm unclear as to where to place the code common to a section. Is splitting the section content into partial views the only solution? So it becomes?
Layout.cshtml -> page.cshtml -> any-number-of-partial-views
To give an example, if I need a single CSS file to be shared across all 10 section pages, I can put it in a partial view and render it on each page but doesn't feel incredibly efficient. Or maybe I just need to get my head around to this new way of working.
You can use sections in MVC as well.
In your _Layout.cshtml you might have something like this:
#RenderSection("testSection",required:false)
And in your child view you can specify what HTML should be rendered in this section or you can leave it all together because required has been set to false:
#section testSection{
<h1>Test</h1>
}
Partial view is your big friend if you would like to reuse the code several places.
You can put partial view inside Views > ControllerNameAsFolderName > view pages (put here) to use only in same controller views or even inside Shared folder to use globally.
section is another option, this is like UserControl (in web forms). create section reference #RenderSection("sectionName", required:false) on Layout page and use it on view pages like
#section sectionName{
<div>content goes here</div>
}
Assume you want to put something (like meta infos) inside <head> from view pages, in this case you can create a section in _Layout.cshtml page and then use that section on view pages.
This way you can place your code at certain location in DOM structure from any page. section has a great feature that allow you to mark it as required:false/true.

Ways to keep the same header on every page of the website

I am trying to make a member based website in which I will need to keep the HEADER and certain other elements fixed on every page of the site.
I am familiar with the concept of MasterPages in Asp.net, and SHTML however I find it a bit confusing and tedious to use them for a website.
I want to know that are their some other ways to achieve the same feat in a way other than SHTML or MasterPages, one that is more refined and easy to implement?
Create User Control In your Application and Register Your User Control in your Page(like .aspx).
<%# Register TagPrefix="UC" Src="~/UserControl.ascx" TagName="mycontrol" %>
and Use Like control.
<UC:mycontrol ID="my" runat="server" />
You need to get acquainted with the concept of UserControls. Read here for a complete understanding: MSDN LINK
User controls are powerful functionality of ASP.NET. With the help of the user control you can reuse the Design as well as code in the application.
This another link provides very basic introduction of UserControls and finally ends up creating a one:http://asp.net-tutorials.com/user-controls/introduction/
I am currently using a perl script that concatenates header/content/footer files together. However this is a bit OTT so I am in the process of converting to iframes. All you need do is add a line to the top of each page...
<iframe border=0 src="http://mysite.com/header.html"></iframe>
You can adjust the size of the frame if needed.
If you don't like master pages, you could always try Dreamweaver templates, which auto generates the template contents on each page using the templates.

Is it possible to create new ASP.NET directives?

You know how ASP.NET pages typically begin with a Page directive setting certain properties and behaviors for the page? Looks like this:
<%# Page Title="Contact Us" Language="C#" MasterPageFile="~/Site.master"
AutoEventWireup="true" CodeFile="ContactUs.aspx.cs" Inherits="ContactUs" %>
The whole list of currently available directives is here:
http://msdn.microsoft.com/en-us/library/t8syafc7.aspx
Does anyone know if it is possible to create new directives?
For example, I'm using reflection to do certain things with pages on the site and I decorate their class declarations with custom attributes. I'd like to know if it's possible to somehow do that even when there isn't a code behind or designer file to find such a class declaration, and all the scripts for the page are in the script tag that's run on the server side.
P.S. I'm aware that I could use the Page directive to set the base class and then also use it to set inherited properties. However, there can be 0 - n of these attributes, so that couldn't be done elegantly.

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