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

Answer

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.

All asp Questions

Ask your interview questions on asp

Write Your comment or Questions if you want the answers on asp from asp Experts
Name* :
Email Id* :
Mob no* :
Question
Or
Comment* :
 





Disclimer: PCDS.CO.IN not responsible for any content, information, data or any feature of website. If you are using this website then its your own responsibility to understand the content of the website

--------- Tutorials ---