We take pride in your success. We let our positivity drive us, day in and out. Talk to us at Mindfire to know us more.

Software Technology Tips

If you have Master page for your website then you must be needing reference to its controls from the child content page. There are various ways to do it. To get strong access between master and content page you can add @ MasterType directive to your content page. For example if your Page directive has masterPageFile = " ~/MyMasterPage.master" , then you have  to add following line to aspx page.

<%@ MasterType  virtualPath=~/MyMasterPage.master" %>

Now you can access public properties of master page from content page. To locate specific control we can use FindControl method:
 
Label myHeaderLabel = (Label) Master.FindControl("lblmasterHeader"); //  lblmasterHeader is the ID of label
But do note one more thing. If the control is inside a ContentPlaceHolder then you have to handle it separately. When at run time content page merges with the Master page layout then IDs of the controls change. Suppose actual ID of a textbox is "txtUserName" then it will be render as something like "ctl00_ContentPlaceHolder1_txtUserName" where ContentPlaceHolder1 is the ID of ContentPlaceHolder. So we have to first find the ContentPlaceHolder then textbox hierarchically. e.g.
ContentPlaceHolder cph;
TextBox myTextBox;
cph= (ContentPlaceHolder)Master.FindControl("ContentPlaceHolder1");
if(cph != null) 
{
    myTextBox = (TextBox) cph.FindControl("txtUserName");
    if(myTextBox != null)
    {
        myTextBox.Text = "TextBox found inside content place holder!”
    }
}
 
Hope you got the basic idea. 
 


Related Tags:

ASP.NET

Author: Anita Bhanja

top

ASP.NET

Let us Connect!

privacy

copyright (c) Mindfire Solutions 2007-2012. Login