【发布时间】:2011-04-11 12:48:21
【问题描述】:
我想从我的 .ascx 中引用我的 .aspx 中的一个属性。
假设我的页面类是:
public partial class MyAdminPage : System.Web.UI.Page
我期待我可以从我的 .ascx 中完成:
MyAdminPage myPageInstance = (MyAdminPage)this.Page;
或
MyAdminPage myPageInstance = this.Page as MyAdminPage;
我不能。
如何从 .ascx 引用我的页面类?
更新 1
感谢您的回复。
我忘了提到 .aspx 有一个母版页,在 contentPlaceholder 中我有 .ascx 控件。
如果我这样做,请从我的 .ascx 中获取:
this.Parent --> 我得到了对“ContentPlaceHolder”的引用
this.Parent.Parent --> 我得到一个对“HtmlForm”的引用
this.Parent.Parent --> 对“MasterPage”的引用。
我不会在任何其他 .aspx 页面中仅重用 .ascx。
【问题讨论】:
-
我认为您有点忽略了用户控件的要点:用户控件是一个可以在多个网页中使用的组件。它类似于您的对象模型中的类。一旦您的组件知道包含页面 - 您不能再在多个页面中使用它,并且它不是真正的用户控件。我认为您有两个选择:1. 放弃用户控件,将其作为网页中的常规控件,2. 重新设计您的用户控件,使其不必了解包含页面。
-
@sJhonny 谢谢,在这种情况下,我将仅在单个 .aspx 页面中使用此 .ascx 控件。我不会重复使用它。
标签: asp.net user-controls