Tuesday, July 7, 2009

How To Access Content From MasterPage in ASP.NET

hi , some time we required some content from master page.
lets say we have created menu bar in Master page and i want to use that menu bar in some page
consider(Details.aspx) and my master page is ("MasterPanel.master) , now how to access master page content
to display the Menu.

MasterPanel.master

<%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPanel.master.cs" Inherits="MasterPanel" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>: Master Demo: </title>
</head>
<body>
<form id="form1" runat="server">
<table border="0" style="width: 100%">
<tr>
<td style="width: 100%">
<ul>
<li>File</li>
<ul>
<li>Open </li>
<li>Save </li>
</ul>
<li>Logout</li>
</ul>
</td>
</tr>
</table>
</form>
</body>
</html>



Details.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Details.aspx.cs"
Inherits="Details" <b><font color=red>MasterPageFile="~/MasterPanel.master"</font></b> %>
<asp:Content ID="Det" runat="Server" ContentPlaceHolderID="ContentPlaceHolder1">

//Menu source will come

</asp:Content>

in Details.aspx we have add MasterPageFile as "~/MasterPanel.master" so it will inclue all the
content from master page . by using <asp:Content> you can place that menu where you want in
Details.aspx page

That's it

Happy coding ......

1 comment:

  1. yes it simple and very helpful for me
    thx
    but i have one problem how i can define that on my regular page

    ReplyDelete