Just as the subject asks.
EDIT 1
Maybe it\'s possible sometime while the request is being processed to store a reference to the parent page in the user contr
You must use NamingContainer like that:
try
{
if (!string.IsNullOrWhiteSpace(TargetCtrlID))
{
var ctrl = NamingContainer.FindControl(TargetCtrlID);
if(ctrl != null)
Console.Write("'" + ctrl.ClientID + "'");
}
}
catch
{
}
Writing to Page and Master Page from Web User Control: Personally I like the user controls to be loose, but it can be done like this.
Master Page:
public partial class Second : System.Web.UI.MasterPage
{
public void SecondMasterString(string text)
{
MasterOut.Text += text;
}
}
Directive needed on WebForm1 : So page can write to master page
<%@ MasterType VirtualPath="~/DemoFolder/MasterPages/Second.master" %>
Methods write to page and Master Page:
public partial class WebForm1 : System.Web.UI.Page
{
public void SetPageOutput(string text)
{
// writes to page
this.PageOut.Text = text;
}
public void SetMaster(string text)
{
// writes to Master Page
this.Master.SecondMasterString(text);
}
}
User Control writes to both Page and Master Page:
public partial class WebUserControl1 : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
LegoLand.DemoFolder.MasterPages.WebForm1 page = (WebForm1)this.Parent.Page;
page.SetMaster("** From the control to the master");
page.SetPageOutput("From the control to the page");
}
}
Every control has a parent property that you can use to access the parent.
protected void Page_Load(object sender, EventArgs e)
{
Response.Write(this.Parent.ID);
}
EDIT: depends on which one of the lifecycle events of the page you want to store the reference and what use to store that reference. However the reference is always available