Click here to Skip to main content
15,903,724 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
After moving from second page of my project,i want to set value in label control of master page.

please suggest me some idea.

I have tried following code,but it throws an error

Object reference not set to an instance of an object.

What I have tried:

I have tried following code:-


Label lblSubHd = this.Master.FindControl("lblSubHd") as Label;
lblSubHd.Visible = true;
Posted
Updated 17-Jul-17 2:33am
Comments
F-ES Sitecore 17-Jul-17 10:59am    
That's the code to use, if it's not working then there is no control called lblSubHd on the master page, or if there is one it is in a container. If it's in a container control you need to FindControl the container then do FindControl on the container to find your label. Without knowing the master page mark-up it's impossible to answer.

1 solution

Hi,

You do not directly access a component of the main page. Its assignatury is of the protected type.

For you access the label component, I suggest that you use public properties in your master page.

HTML code in your master page
ASP.NET
<div>
    <asp:Label ID="lblSubHd" runat="server" Text="Um Texto"></asp:Label>
    <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
   
    </asp:ContentPlaceHolder>
</div>


To access the component, here, the code.
Code-behind to master page
C#
public Label MyLabel
{
   get
   {
      return this.lblSubHd;
   }
}

Code-behind to child page
C#
Label mylabelchild = this.Master.MyLabel;


To access the text property of the label component, here, the code.
Code-behind to master page
C#
public string ValueLabel
{
    get
    {
      return this.lblMyLabel.Text;
    }
}

Code-behind to child page
C#
this.MylabelChild.Text = this.Master.ValueLabel;


IMPORTANT! For you use the solution above your child page need to have the master page in asp tag.
ASP.NET
<%@ Page Language="C#" AutoEventWireup="true" MasterPageFile="~/Main.Master" CodeBehind="child_page.aspx.cs" Inherits="aplicacao.child_page" %>
<%@ MasterType VirtualPath="~/Main.Master" %>
 
Share this answer
 
v6

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900