Download introduction to master pages

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

URL redirection wikipedia , lookup

Transcript
INTRODUCTION TO MASTER PAGES
Adding Master Pages:
ASP.NET master pages allow you to create a consistent layout for the pages in your application. A single
master page defines the look and feel and standard behavior that you want for all of the pages (or a group of
pages) in your application.
->You can then create individual content pages that contain the content you want to display. When users
request the content pages, they merge with the master page to produce output that combines the layout of the
master page with the content from the content page.
How Master Pages Work:
Master pages actually consist of two pieces, the master page itself and one or more content pages.
A master page is an ASP.NET file with the extension .master (for example, MySite.master) with a predefined
layout that can include static text, HTML elements, and server controls. The master page is identified by a
special @ Master directive that replaces the @ Page directive that is used for ordinary .aspx pages. The directive
looks like the following.
<%@ Master Language="C#" %>
The @ Master directive can contain most of the same directives that a @ Control directive can contain. For
example, the following master-page directive includes the name of a code-behind file, and assigns a class name
to the master page.
<%@ Master Language="C#" CodeFile="MasterPage.master.cs" Inherits="MasterPage" %>
Replaceable Content Placeholders
In addition to static text and controls that will appear on all pages, the master page also includes one or more
ContentPlaceHolder controls. These placeholder controls define regions where replaceable content will appear. In
turn, the replaceable content is defined in content pages. After you have defined the ContentPlaceHolder
controls, a master page might look like the following.
<%@ Master Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML
1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server" >
<title>Master page title</title>
</head>
<body>
<form id="form1" runat="server">
<table>
<tr>
<td><asp:contentplaceholder id="Main" runat="server" /></td>
<td><asp:contentplaceholder id="Footer" runat="server" /></td>
</tr>
</table>
</form>
</body>
</html>
Selecting Master Pages to the Webforms
You use ASP.NET Web pages as the programmable user interface for your Web application. An ASP.NET Web
page presents information to the user in any browser or client device and implements application logic using
server-side code. ASP.NET Web pages are:





Based on Microsoft ASP.NET technology, in which code that runs on the server dynamically generates
Web page output to the browser or client device.
Compatible with any browser or mobile device. An ASP.NET Web page automatically renders the
correct browser-compliant HTML for features such as styles, layout, and so on.
Compatible with any language supported by the .NET common language runtime, such as
Microsoft Visual Basic and Microsoft Visual C#.
Built on the Microsoft .NET Framework. This provides all the benefits of the framework, including a
managed environment, type safety, and inheritance.
Flexible because you can add user-created and third party controls to them.
What ASP.NET Web Pages Help You Accomplish
Web application programming presents challenges that do not typically arise when programming traditional
client-based applications. Among the challenges are:



Implementing a rich Web user interface It can be difficult and tedious to design and implement a
user interface using basic HTML facilities, especially if the page has a complex layout, a large amount of
dynamic content, and full-featured user-interactive objects.
Separation of client and server In a Web application, the client (browser) and server are different
programs often running on different computers (and even on different operating systems).
Consequently, the two halves of the application share very little information; they can communicate,
but typically exchange only small chunks of simple information.
Stateless execution When a Web server receives a request for a page, it finds the page, processes it,
sends it to the browser, and then discards all page information. If the user requests the same page
again, the server repeats the entire sequence, reprocessing the page from scratch. Put another way, a



server has no memory of pages that it has processed—page are stateless. Therefore, if an application
needs to maintain information about a page, its stateless nature can become a problem.
Unknown client capabilities In many cases, Web applications are accessible to many users using
different browsers. Browsers have different capabilities, making it difficult to create an application that
will run equally well on all of them.
Complications with data access Reading from and writing to a data source in traditional Web
applications can be complicated and resource-intensive.
Complications with scalability In many cases Web applications designed with existing methods fail
to meet scalability goals due to the lack of compatibility between the various components of the
application. This is often a common failure point for applications under a heavy growth cycle.
Adding Navigation Controls (Menus,Tree view)to Master Page:
Navigation with SiteMap, Menu and TreeView:
Site navigation interfaces have always been a complicated subject. What should we use to
create a user-friendly interface, and at the same time, easy to maintain? ASP.NET 2.0 has
brought resources that made this task so simple it will look like a joke . This article will
explain how Master Pages, Site Map, Menus and TreeView can help us to create navigation
interfaces with not even one code line!
Master Pages
Create a new Web Site in Visual Studio 2005. Close Default.aspx page and through Solution
Explorer add a new item. In the Add New Item window choose the Master Page template
and name it “Menu.Master”, as shown in Figure 1.
A Master Page is nothing else than an ASP.NET page. The difference is that a Master Page
comes with a control called ContentPlaceHolder. We will easily understand the behavior of a
Master Page as soon we add new pages to the project. For the time being, think of it as
being the site’s “main page” and the ContentPlaceHolder control, the space reserved for the
content of the site, which will be modified while we navigate.
Place the cursor before ContentPlaceHolder and add a new HTML Table to the page, through
the Layout> Insert Table menu. A very interesting feature of Visual Studio 2005 is the
template option that appears when we are creating tables (Figure 2). Note that there are
many common templates to build Web interfaces. Select template and choose Header and
Side.
Drag the ContentPlaceHolder into the central cell of the table, as seen in Figure 3. Adjust
spaces, borders, colors etc. so the page will suit you. Consider that this will be the visual
identity of your entire site, and that the ContentPlaceHolder control must be set where the
content of the site will appear.
After formatting your master page, create a new Web Form through the Add New Item
option, which you access by right clicking the mouse over the project in Solution Explorer.
Type the name “Start.aspx” and check the select Master Page option (Figure 4).
Next, a window will appear so that you can choose which the master page of this content
page is. Choose Menu.Master and click OK (Figure 5). The Start.aspx page will be created.
See in the page design that the only area in which we can work is exactly the space
reserved by the master page’s ContentPlaceHolder. Our content page stays covered with the
formats performed in the master page. In the Start.aspx page, add a text to identify the
page. If you wish, you can realize any other formatting or add other controls. The behavior
of this page is the same as an ASPX traditional page.
Adding TreeView control to the Master Page
Try to navigate in the site through the TreeView. Note that as we choose an option through
TreeView, automatically the respective selected option of the Menu is modified. The
SiteMapPath is also modified independently of which control we use to navigate.
WRITING APPLICATIONS
Code-Behind
Code-behind refers to code for your ASP.NET page that is contained within a separate class file. This allows
a clean separation of your HTML from your presentation logic. The following sample illustrates an ASP.NET
code-behind page:
MyCodebehind.aspx
<%@ Language="C#" Inherits="MyStuff.MyClass" %>
<HTML>
<body>
<form id="MyForm" runat="server">
<asp:textbox id="MyTextBox" text="Hello World" runat="server"></asp:textbox>
<asp:button id="MyButton" text="Echo Input" Onclick="MyButton_Click"
runat="server"></asp:button>
<asp:label id="MyLabel" runat="server" />
</form>
</body>
</HTML>
Mycodebehind.cs
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace MyStuff
{
public class MyClass : Page
{
protected System.Web.UI.WebControls.Label MyLabel;
protected System.Web.UI.WebControls.Button MyButton;
protected System.Web.UI.WebControls.TextBox MyTextBox;
public void MyButton_Click(Object sender, EventArgs e)
{
MyLabel.Text = MyTextBox.Text.ToString();
}
}
}