Thursday, January 2, 2014

How to access ContentPage controls in master page and vice versa

void Page_Load()
{
   
// Gets a reference to a TextBox control inside
   
// a ContentPlaceHolder
   ContentPlaceHolder mpContentPlaceHolder;
   TextBox mpTextBox;
   mpContentPlaceHolder =
     (ContentPlaceHolder)Master.FindControl(
"ContentPlaceHolder1");
   
if(mpContentPlaceHolder != null)
   {
       mpTextBox =
           (TextBox) mpContentPlaceHolder.FindControl(
"TextBox1");
       
if(mpTextBox != null)
       {
           mpTextBox.Text =
"TextBox found!";
       }
   }
   
   
// Gets a reference to a Label control that not in
   
// a ContentPlaceHolder
   Label mpLabel = (Label) Master.FindControl(
"masterPageLabel");
   
if(mpLabel != null)
   {
       Label1.Text =
"Master page label = " + mpLabel.Text;
   }
}

No comments:

Followers

Link