Just as the subject asks.
正如题目问的那样。
EDIT 1
编辑1
Maybe it's possible sometime while the request is being processed to store a reference to the parent page in the user control?
也许在处理请求时,可以在user控件中存储对父页面的引用?
8 个解决方案
#1
57
this.Page
or from just about anywhere:
或者来自任何地方:
Page page = HttpContext.Current.Handler as Page
#2
11
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
.
也就是说,你可以用这个。page。
#3
7
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");
#4
2
I always used this.Page in the System.Web.UI.UserControl.
我总是用这个。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);
}
#5
2
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.
我发现这样做的方法是创建一个接口,实现这个接口,使用这个。要从控件获取页面,请将其转换为接口,然后调用该方法。
#6
1
You must use NamingContainer like that:
你必须使用像这样的NamingContainer:
try
{
if (!string.IsNullOrWhiteSpace(TargetCtrlID))
{
var ctrl = NamingContainer.FindControl(TargetCtrlID);
if(ctrl != null)
Console.Write("'" + ctrl.ClientID + "'");
}
}
catch
{
}
#7
0
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
编辑:取决于要存储引用的页面的生命周期事件之一,以及存储引用的用途。但是引用总是可用的
#8
0
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
}
}
#1
57
this.Page
or from just about anywhere:
或者来自任何地方:
Page page = HttpContext.Current.Handler as Page
#2
11
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
.
也就是说,你可以用这个。page。
#3
7
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");
#4
2
I always used this.Page in the System.Web.UI.UserControl.
我总是用这个。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);
}
#5
2
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.
我发现这样做的方法是创建一个接口,实现这个接口,使用这个。要从控件获取页面,请将其转换为接口,然后调用该方法。
#6
1
You must use NamingContainer like that:
你必须使用像这样的NamingContainer:
try
{
if (!string.IsNullOrWhiteSpace(TargetCtrlID))
{
var ctrl = NamingContainer.FindControl(TargetCtrlID);
if(ctrl != null)
Console.Write("'" + ctrl.ClientID + "'");
}
}
catch
{
}
#7
0
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
编辑:取决于要存储引用的页面的生命周期事件之一,以及存储引用的用途。但是引用总是可用的
#8
0
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
}
}