<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://weblogs.asp.net/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Vikram Lakhotia : Master Page</title><link>http://weblogs.asp.net/vikram/archive/tags/Master+Page/default.aspx</link><description>Tags: Master Page</description><dc:language>en</dc:language><generator>CommunityServer 2007 SP1 (Build: 20510.895)</generator><item><title>How to refer Master page content in Content Page</title><link>http://weblogs.asp.net/vikram/archive/2008/02/27/how-to-refer-master-page-content-in-content-page.aspx</link><pubDate>Wed, 27 Feb 2008 03:44:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:5867333</guid><dc:creator>vik20000in</dc:creator><author>vik20000in</author><slash:comments>8</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/vikram/rsscomments.aspx?PostID=5867333</wfw:commentRss><comments>http://weblogs.asp.net/vikram/archive/2008/02/27/how-to-refer-master-page-content-in-content-page.aspx#comments</comments><description>&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT face="Times New Roman" size=3&gt;Hi,&lt;/FONT&gt;&lt;/P&gt;&lt;?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /&gt;&lt;o:p&gt;&lt;FONT face="Times New Roman" size=3&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/o:p&gt; 
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT face="Times New Roman" size=3&gt;Many a times while using a master page we want to refer content, Controls and properties in Master Page while working in Content page. We come across requirement where by we can change or access the values of controls residing in Master page when we are coding in the content page itself.&lt;/FONT&gt;&lt;/P&gt;&lt;o:p&gt;&lt;FONT face="Times New Roman" size=3&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/o:p&gt; 
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT face="Times New Roman" size=3&gt;We can write code in the content pages to access properties, methods and controls in the master page, but there are some limits to it. We can only work with those properties and methods which are declared public. For controls we can refer any control on the master page independent of referencing public member.&lt;/FONT&gt;&lt;/P&gt;&lt;o:p&gt;&lt;FONT face="Times New Roman" size=3&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/o:p&gt; 
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT face="Times New Roman" size=3&gt;We can use the @MasterPage directive in the aspx to make the Master property of the content type to be strongly typed. This helps in writing code fro public properties and methods of the master page to be easily coded in compile time. &lt;/FONT&gt;&lt;/P&gt;&lt;o:p&gt;&lt;FONT face="Times New Roman" size=3&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/o:p&gt; 
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT face="Times New Roman" size=3&gt;To refer a control I the master page we need to use the FindControl method of the Master property. The controls in the master page can be inside the content place holder in the master page or outside the content place holder of the master page. Both can be easily referred in the content page. For referring to control inside the content place holder in side Master page we first need to refer the content placeholder and then use its FindControl method. Here is how to do it.&lt;/FONT&gt;&lt;/P&gt;&lt;o:p&gt;&lt;FONT face="Times New Roman" size=3&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/o:p&gt; 
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT face="Times New Roman" size=3&gt;// for control Inside Content Placeholder&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT face="Times New Roman" size=3&gt;ContentPlaceHolder masterPagePlaceHolder;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT face="Times New Roman" size=3&gt;TextBox masterPageTextBox;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT face="Times New Roman" size=3&gt;masterPagePlaceHolder =(ContentPlaceHolder)Master.FindControl("ContentPlaceHolder1");&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT face="Times New Roman" size=3&gt;if(masterPagePlaceHolder != null)&lt;BR&gt;{&lt;BR&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;masterPageTextBox =(TextBox) masterPagePlaceHolder.FindControl("TextBox1");&lt;BR&gt;}&lt;/FONT&gt;&lt;/P&gt;&lt;o:p&gt;&lt;FONT face="Times New Roman" size=3&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/o:p&gt; 
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT face="Times New Roman" size=3&gt;// for control outside Content Placeholder&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT face="Times New Roman" size=3&gt;Label masterPageLabel = (Label) Master.FindControl("masterPageLabel");&lt;/FONT&gt;&lt;/P&gt;&lt;o:p&gt;&lt;FONT face="Times New Roman" size=3&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/o:p&gt; 
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT face="Times New Roman" size=3&gt;Vikram&lt;/FONT&gt;&lt;/P&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=5867333" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/vikram/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/vikram/archive/tags/Master+Page/default.aspx">Master Page</category><category domain="http://weblogs.asp.net/vikram/archive/tags/Content+Page/default.aspx">Content Page</category><category domain="http://weblogs.asp.net/vikram/archive/tags/Master/default.aspx">Master</category><category domain="http://weblogs.asp.net/vikram/archive/tags/FindControl/default.aspx">FindControl</category></item></channel></rss>