Introduction
I have already discussed about Master Page Properties Exposing in my previous, in that article we have already discussed how to modify a property
of a control located in a Master Page from a content page by exposing a property
from the Master Page. We have an alternative here by using Find Control with
Master Page. If we need to modify a control in a Master Page, we can use the
FindControl() method in a content page. For example, the Master Page given below
includes a Literal control named MasterBodyTitle. This Master Page does not
include any custom properties by default.
Master Page Code
<%@
Master Language="VB"
%>
<!DOCTYPE
html PUBLIC
"-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script
runat="server">
Public
Property MasterBodyTitle()
As String
Get
Return
ltlMasterBodyTitle.Text
End Get
Set(ByVal
Value As String)
ltlMasterBodyTitle.Text = Value
End Set
End
Property
</script>
<html
xmlns="http://www.w3.org/1999/xhtml">
<head
id="Head1"
runat="server">
<style
type="text/css">
html
{
background-color:silver;
}
.content
{
margin:auto;
width:700px;
background-color:white;
padding:10px;
}
h1
{
border:3px
dotted blue;
}
</style>
<title>Using
Find Control with Master Page</title>
</head>
<body>
<form
id="form1"
runat="server">
<div
class="content">
<h2><asp:Literal
ID="ltlMasterBodyTitle"
runat="server"
/></h2>
<asp:ContentPlaceHolder
id="ContentPlaceHolder1"
runat="server">
</asp:ContentPlaceHolder>
</div>
</form>
</body>
</html>
Default.aspx Code
<%@
Page Title=""
Language="VB"
MasterPageFile="~/MasterPage.master"
%>
<script
runat="server">
Protected
Sub Page_Load(ByVal
sender As Object,
ByVal e As
System.EventArgs)
If Not
Page.IsPostBack Then
Dim ltlMasterBodyTitle
As
Literal = CType(Master.FindControl("ltlMasterBodyTitle"),
Literal)
ltlMasterBodyTitle.Text =
"This title is from content page (this is from content page for Master Page)."
End
If
End Sub
</script>
<asp:Content
ID="Content2"
ContentPlaceHolderID="ContentPlaceHolder1"
Runat="Server">
There is some data for the page body (this is from content page).
</asp:Content>
The FindControl() method enables us to search a naming container for a control
with a particular ID. The method returns a reference to the control.
HAVE A GREAT CODING!