PDA

View Full Version : How can I access to Master page's elements?


pasquale
07-11-2007, 10:52 AM
Hi all,

please I need your help. Im completed stucked on something that Im not able to do in VB .NET....

I created a master page containing this Literal in the Head:

<head runat="server">
<title><asp:Literal id="harriertitle" runat="server"></asp:Literal></title>
<vccp:meta ID="pageMeta" runat="server" />
</head>

Then I create a page container that use this master page, and I need to change in the Page_Load the value of the Literal "harriertitle" with the title of the page, this is the backend code:

Partial Class materPages_landing
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

harriertitle.Text = "my title" <-- this is wrong!!, because harriertitle is in Master page not in landing.aspx.............??
Master.FindControl("harriertitle") ???? <-- this is wrong!!
How can I access to the Literal whose id is "harriertitle"?????

End Sub

End Class


This is the front-end code:

<%@ Page Language="VB" MasterPageFile="~/materPages/main.master" AutoEventWireup="false" CodeFile="landing.aspx.vb" Inherits="materPages_landing" title="Untitled Page" %>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
</asp:Content>

How can the landing.aspx change the value of the literal in the Master??

Please help me..I need urgently your help... Otherwise Im going to lose my new job.....

Please answer me as soon as possible...

Many Thanks

TheShaner
07-11-2007, 04:02 PM
Put this in your master page:
public string harriertitletext
{
set { harriertitle.Text = value; }
}
Access it with this in your landing.aspx page:
Master.harriertitletext = "hidden";

I found this code from this thread (http://codingforums.com/showthread.php?t=116840) actually :D

-Shane

pasquale
07-13-2007, 03:59 PM
Thank you very much!

Your post helped me a lot to find the way to do it,
Basically your example was in C#, in VB.NET is a bit different, but it's the same principle, I wrote this in the masterpage.vb:

Private m_harriermarker As String
Private m_harriertitle As String

Public Property harriermarker() As String
Get
Return m_harriermarker
End Get
Set(ByVal value As String)
m_harriermarker = value
End Set
End Property

Public Property harriertitle() As String
Get
Return m_harriertitle
End Get
Set(ByVal value As String)
m_harriertitle = value
End Set
End Property

And then I wrote this in the landingpage.aspx.vb :

'Set the values for page title and page marker here:
Dim getMaster As MasterPage = Me.Master
getMaster.harriertitle = "Landing Page | Prize draw | Pilsner Urquell"
getMaster.harriermarker = "landing"

Basically because the landing page is using the master page MasterPage:

<%@ Page Language="VB" MasterPageFile="~/MasterPage.master" AutoEventWireup="false" CodeFile="enter.aspx.vb" Inherits="enter" %>

Then, in order to access to the all properties of the class MasterPage ( harriermarker, harriertitle, .... ) I needed to assign Me.Master to a variable declared As MasterPage, otherwise I was never able to see those properties....

Dim getMaster As MasterPage = Me.Master

Basically the methods Get and Set are a fantastic way to extend the behave of the basic classes in .NET. I think that it should work for client controls as well (ascx). Somebody can tell me please if it's alright? Do you have some example in C# or VB about the methods Get and Set?