top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How you can access the Properties and Controls of Master Pages from content pages?

+1 vote
285 views
How you can access the Properties and Controls of Master Pages from content pages?
posted Jan 6, 2015 by Jayshree

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

You can access the Properties and Controls of Master Pages from content pages. In many situations you need User’s Name in different content pages. You can set this value inside the master page and then make it available to content pages as a property of the master page.

We will follow the following steps to reference the properties of master page from content pages.

STEP 1:

Create a property in the master page code-behind file.

public String UserName 
{
get {
    return (String)Session["Name"];
}
set {
Session ["Name"] = value;
}
}

STEP: 2

Add the @ MasterTypedeclaration to the .aspx content page to reference master properties in a content page. This declaration is added just below the @ Page declaration as follows:

<%@ Page Title=" TEST" Language="C#" MasterPageFile="~/CareerRide.master" AutoEventWireup="true" CodeFile="CareerRideWelcome.aspx.cs" Inherits="CareerRideWelcome" %>

<%@ MasterTypeVirtualPath="~/CareerRide.master" %>

STEP: 3

Once you add the @ MasterType declaration, you can reference properties in the master page using the Master class. For example take a label control that id is ID="Label1"

Label1.Text= Master.UserName ;

For referencing controls in the Master Page we will write the following code.

Content Page Code.

protected void Button1_Click(object sender, EventArgs e)
{
TextBox txtName= (TextBox)Master.FindControl("TextBox1");
Label1.Text=txtName.Text; 
}

To reference controls in a master page, call Master.FindControl from the content page.

answer Jan 7, 2015 by Shivaranjini
Similar Questions
+3 votes

How do we retrieve the customized properties of a .NET application from XML .config file? Can we automate this process?

+4 votes

When using aspx view engine, to have a consistent look and feel, across all pages of the application, we can make use of asp.net master pages. What is asp.net master pages equivalent, when using razor views?

...