Finally..on public demand..Access master page control from content page with all the details!!!
The common method to access master page control (in this example lable) from content page is given below:
Dim myLabel As Label
myLabel = CType(Master.FindControl("masterpageLabel"), Label)
If Not mpLabel Is Nothing Then
Label1.Text = "Master page label = " + myLabel.Text
End If
To demonstrate how we create and use these Properties, imagine we have a Master Page which contains a Label control (we'll just call it "Label1"), and we want to show it's value in a TextBox on our Content Page. The Master Page, would look like this:
<%@ Master Language="VB" CodeFile="site.master.vb" Inherits="site" %>
<!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 runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="Testing"></asp:Label>
<asp:contentplaceholder id="ContentPlaceHolder1" runat="server">
</asp:contentplaceholder>
</div>
</form>
</body>
</html>
Content Page would look like this:
<%@ Page Language="VB" MasterPageFile="~/site.master" AutoEventWireup="false" CodeFile="Default1.aspx.vb" Inherits="Default1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<asp:TextBox ID="txtTest" runat="server"></asp:TextBox>
</asp:Content>
Now, we need some way of getting the Master Page to expose the Text property of it's "Label1" control. This is where the Properties come in and to set this up we need to make the Property Public so that it can be accessed by other pages. So, our code behind for the Master Page will now consist of:
Partial Class site
Inherits System.Web.UI.MasterPage
Public Property MyText()
Get
Return Label1.Text
End Get
Set(ByVal value)
Label1.Text = value
End Set
End Property
End Class
You'll notice that there is both a Get and a Set method for this Property which will allow us to both retrieve and update the value. To do this from our Content Page, we first need to get a reference to the Master Page, which can be done easily by using the Page.Master property, and then we need to cast the Master Page to the type of Master we are using so that we can see the Public Property and Get the value. We can also use the same methods to Set the property, as is shown by looking at the code behind for the Content Page:
Partial Class Default1
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
' Set the value of the Master Page's Label to the Content Page's TextBox
txtTest.Text = CType(Page.Master, site).MyText
' Update the Master Page's Label
CType(Page.Master, site).MyText = "Changed"
End Sub
End Class
When you run this code in your development environment, you'll see that the first Label shows our "Changed" text, whereas the TextBox will show the previous value which we retrieved via the Get method.
Dim myLabel As Label
myLabel = CType(Master.FindControl("masterpageLabel"), Label)
If Not mpLabel Is Nothing Then
Label1.Text = "Master page label = " + myLabel.Text
End If
To demonstrate how we create and use these Properties, imagine we have a Master Page which contains a Label control (we'll just call it "Label1"), and we want to show it's value in a TextBox on our Content Page. The Master Page, would look like this:
<%@ Master Language="VB" CodeFile="site.master.vb" Inherits="site" %>
<!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 runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="Testing"></asp:Label>
<asp:contentplaceholder id="ContentPlaceHolder1" runat="server">
</asp:contentplaceholder>
</div>
</form>
</body>
</html>
Content Page would look like this:
<%@ Page Language="VB" MasterPageFile="~/site.master" AutoEventWireup="false" CodeFile="Default1.aspx.vb" Inherits="Default1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<asp:TextBox ID="txtTest" runat="server"></asp:TextBox>
</asp:Content>
Now, we need some way of getting the Master Page to expose the Text property of it's "Label1" control. This is where the Properties come in and to set this up we need to make the Property Public so that it can be accessed by other pages. So, our code behind for the Master Page will now consist of:
Partial Class site
Inherits System.Web.UI.MasterPage
Public Property MyText()
Get
Return Label1.Text
End Get
Set(ByVal value)
Label1.Text = value
End Set
End Property
End Class
You'll notice that there is both a Get and a Set method for this Property which will allow us to both retrieve and update the value. To do this from our Content Page, we first need to get a reference to the Master Page, which can be done easily by using the Page.Master property, and then we need to cast the Master Page to the type of Master we are using so that we can see the Public Property and Get the value. We can also use the same methods to Set the property, as is shown by looking at the code behind for the Content Page:
Partial Class Default1
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
' Set the value of the Master Page's Label to the Content Page's TextBox
txtTest.Text = CType(Page.Master, site).MyText
' Update the Master Page's Label
CType(Page.Master, site).MyText = "Changed"
End Sub
End Class
When you run this code in your development environment, you'll see that the first Label shows our "Changed" text, whereas the TextBox will show the previous value which we retrieved via the Get method.
No comments:
Post a Comment