From the content page code how can you reference a control on the master page?

 Posted by deccansoft on 10/30/2010 | Category: ASP.NET Interview questions | Views: 1483


Use the FindControl() method as shown in the code sample below.
void Page_Load()
{ // Gets a reference to a TextBox control inside // a ContentPlaceHolder ContentPlaceHolder ContPlaceHldr = (ContentPlaceHolder)Master.FindControl ("ContentPlaceHolder1");
if(ContPlaceHldr != null)
{ TextBox TxtBox = (TextBox)ContPlaceHldr.FindControl("TextBox1"); if(TxtBox != null) { TxtBox.Text = "TextBox Present!"; } } // Gets a reference to a Label control that not in // a ContentPlaceHolder Label Lbl = (Label)Master.FindControl("Label1"); if(Lbl != null) { Lbl.Text = "Lable Present"; } }


Asked In: Many Interviews | Alert Moderator 

Comments or Responses

Login to post response