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 control?
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 control?
this.Page
or from just about anywhere:
Page page = HttpContext.Current.Handler as Page
I cannot think of any good reason for a user control to know anything about the page it is on as the user control should be ignorant of its context and behave predictably regardless of what page it is on.
That being said, you can use this.Page
.
you can use the Parent property
if you need this to find a control on the page then you can use
Label lbl_Test = (Label)Parent.FindControl("lbl_Test");
I always used this.Page in the System.Web.UI.UserControl.
Or you can always do a recursive call on the Parent until u encounter an object that is a Page.
kind of overkill though...
protected Page GetParentPage( Control control )
{
if (this.Parent is Page)
return (Page)this.Parent;
return GetParentPage(this.Parent);
}
I found the way to do this is to create an interface, implement that interface, use this.Page to get the page from the control, cast it to the interface, then call the method.
You must use NamingContainer like that:
try
{
if (!string.IsNullOrWhiteSpace(TargetCtrlID))
{
var ctrl = NamingContainer.FindControl(TargetCtrlID);
if(ctrl != null)
Console.Write("'" + ctrl.ClientID + "'");
}
}
catch
{
}
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
Create a delegate in the user control and then assign it a method from the parent page.
class MyUserControl : UserControl
{
delegate object MyDelegate(object arg1, object arg2, object argN);
public MyDelegate MyPageMethod;
public void InvokeDelegate(object arg1, object arg2, object argN)
{
if(MyDelegate != null)
MyDelegate(arg1, arg2, argN); //Or you can leave it without the check
//so it can throw an exception at you.
}
}
class MyPageUsingControl : Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
MyUserContorlInstance.MyPageMethod = PageMethod;
}
public object PageMethod(object arg1, object arg2, object argN)
{
//The actions I want
}
}
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");
}
}
© 2022 - 2024 — McMap. All rights reserved.