Find user control in master page from child page

Find user control in master page from child page

Reference:

Code behind page:

Partial
Class Default2
       
Inherits System.Web.UI.Page

        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
 
             
Dim myCtrl As MyUserControl
             
Dim contentPLH As ContentPlaceHolder              contentPLH =

CType(Me.Master.Page.Form.FindControl("ContentPlaceHolder2"), ContentPlaceHolder)
 
              myCtrl =
CType(contentPLH.FindControl("MyUserControl1"), MyUserControl)               Response.Write(myCtrl.ID)
      
End Sub

End Class

HTML Source of the Default2.aspx (Note that the code in bold to register the control on the page):

<%@ Page Language="VB" MasterPageFile="~/MyMasterPage.master" AutoEventWireup="false" CodeFile="Default2.aspx.vb" Inherits="Default2" title="Untitled Page" %>
<%
@ Register Src="MyUserControl.ascx" TagName="MyUserControl" TagPrefix="uc1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
     
My Page Hello
</asp:Content>

Master Page:

<%@ Master Language="VB" CodeFile="MyMasterPage.master.vb" Inherits="MyMasterPage" %>
<%
@ Register Src="MyUserControl.ascx" TagName="MyUserControl" TagPrefix="uc1" %>
<!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:contentplaceholder id="ContentPlaceHolder1" runat="server">
                   
</asp:contentplaceholder>
                   
<asp:contentplaceholder id="ContentPlaceHolder2" runat="server">
                             
<uc1:MyUserControl ID="MyUserControl1" runat="server" />
                   
</asp:contentplaceholder>
              
</div>
        
</form>
</
body>
</
html>


 
 
Maint