My shared items

 

Saturday, September 8, 2007

Master Pages Demo






Master Page Design:

<%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="MasterPage" %>
DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Master Pages Demotitle>
head>

<body leftmargin="0" topmargin="0" scroll="no">
<form id="form1" runat="server">
<div>
<table border="0" cellpadding="0" cellspacing="0" style="width: 100%; height: 100%">
<tr style="height: 3%">
<td valign="top" align="right" background="Images/banner.gif">
<table width="100%" style="text-align: right">
<tr>
<td width="38%" valign="bottom">
td>
<td valign="bottom" align="right">

<%--Script_Menu--%>

td>
tr>
table>
td>
tr>
<tr height="97%">
<td align="center">
<table width="99%" height="100%">
<tr style="height: 3%">
<td align="left" bgcolor="#BDB14B" valign="top" style="font: Bold; font-family: Verdana;">

<asp:Label ID="Label1" runat="server" Text="">asp:Label>
td>
tr>
<tr style="height: 94%">
<td>
<asp:ContentPlaceHolder ID="ScreenContent" runat="server">
asp:ContentPlaceHolder>
<asp:Label ID="Label2" runat="server" Text="">asp:Label>
<asp:Label ID="Label3" runat="server" Text="">asp:Label>
td>
tr>
<tr style="height: 3%" bgcolor="#BDB14B">
<td align="center" style="font-family: Verdana; font-size: 12;">
CopyRights

td>
tr>
table>
td>
tr>
table>
div>
form>
body>
html>



Master Page Code-Behind File:

public partial class MasterPage : System.Web.UI.MasterPage
{
string pageHeadingTitle = "Header";

///
/// Property return a string
///
public string PageHeadingTitle
{
get
{
return pageHeadingTitle;
}
set
{
pageHeadingTitle = value;
}
}

///
/// Property returns a Label Object
///
public Label MasterPageLabel
{
get
{
return Label2;
}
set
{
Label2 = value;
}
}

protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
Label1.Text = PageHeadingTitle;
}
}
}

Content Page Design:

<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" Title="Default1" %>
<%@ MasterType VirtualPath="~/MasterPage.master" %>
<asp:Content ID="Content2" ContentPlaceHolderID="ScreenContent" Runat="Server">
asp:Content>



Content Page Code Behind File:

///Using FindControl()
this.MasterPageFile = "MasterPage.master";
MasterPage mp = (MasterPage)this.Master;

Label lbl = mp.FindControl("Label2") as Label;
if (lbl != null)
lbl.Text = "Label2";

///(Or)

(Master.FindControl("Label3") as Label).Text = "Label3";

///Using Properties Or Controls in Master Page
Master.PageHeadingTitle = "Content Page Header";
Master.MasterPageLabel.Font.Size = 25;
Master.MasterPageLabel.Text = "Label2";



No comments: