C# 如何从 ASP.NET 网站(不是 Web 应用程序)中的用户控件获取父页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1028937/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I get the parent page from a User Control in an ASP.NET Website (not Web Application)
提问by JoeB
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?
也许有可能在处理请求时在用户控件中存储对父页面的引用?
回答by Paul Alexander
this.Page
or from just about anywhere:
或来自几乎任何地方:
Page page = HttpContext.Current.Handler as Page
回答by Josh Mein
you can use the Parent property
您可以使用 Parent 属性
if you need this to find a control on the page then you can use
如果你需要它来在页面上找到一个控件,那么你可以使用
Label lbl_Test = (Label)Parent.FindControl("lbl_Test");
回答by Andrew Hare
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
.
话虽如此,您可以使用this.Page
.
回答by Marco Mangia
Every control has a parent property that you can use to access the parent.
每个控件都有一个 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
编辑:取决于您要存储引用的页面生命周期事件之一以及用于存储该引用的内容。但是,参考始终可用
回答by BigBlondeViking
I always used this.Page in the System.Web.UI.UserControl.
我总是在 System.Web.UI.UserControl 中使用 this.Page。
Or you can always do a recursive call on the Parent until u encounter an object that is a Page.
或者,您始终可以对 Parent 进行递归调用,直到遇到一个 Page 对象。
kind of overkill though...
虽然有点矫枉过正……
protected Page GetParentPage( Control control )
{
if (this.Parent is Page)
return (Page)this.Parent;
return GetParentPage(this.Parent);
}
回答by JoeB
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.
我发现这样做的方法是创建一个接口,实现该接口,使用 this.Page 从控件中获取页面,将其转换为接口,然后调用该方法。
回答by Hakan KOSE
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
{
}
回答by Nikolay
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
}
}
回答by Stewart
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.
从 Web 用户控件写入页面和母版页:我个人喜欢用户控件松散,但可以这样做。
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
WebForm1 上需要指令:因此页面可以写入母版页
<%@ 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");
}
}