ASP.NET 用户控件能否确定其上下文或其父 .aspx 文件
用户控件是否可以通过某种方式确定其“上下文”或其父 .aspx 页面?
现在,我有一个在典型 .aspx 页面上声明的用户控件,如下所示:
<%@ Register TagPrefix="uc1" TagName="ManageTitle" Src="../UserControls/ManageTitle.ascx" %>
用户控件当前发出一个文本框,如下所示:
<asp:textbox id="txtTitle" runat="server" MaxLength="60"
ToolTip="Describe the item with a short pithy title - most important keywords first"/>
该 .ascx 文件的 page_load 当前如下所示:
Me.txtTitle.Text = SetPageTitle()
虽然此 Web 应用程序中的某些地方需要此(即最终用户可以在其中输入“标题”的文本框),我还有其他地方想以“只读”方式显示“标题”信息。例如,我可以使用标签控件或带有 Enabled="false" 的文本框来防止 数据输入。
我想我可以克隆这个小 .ascx 文件并在其名称中附加一个后缀,例如 _RO.ascx 或其他内容,但我想知道最好的方法是什么。
简而言之,用户控件是否可以从声明它的页面获取某种“上下文”,或者是否有更好的方法来完成此类事情?谢谢。
-- 使用建议的方法编辑更新 --------------------------
添加到 UserControl 的代码:
Private mIsReadOnly As Boolean
Public Property IsReadOnly() As Boolean
Get
IsReadOnly = mIsReadOnly
End Get
Set(ByVal value As Boolean)
mIsReadOnly = value
End Set
End Property
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Page.IsPostBack Then
'Leave the textbox alone
Else
Me.txtTitle.Text = SetPageTitle() 'This is the original code
If IsReadOnly Then
Me.txtTitle.Enabled = False
Else
Me.txtTitle.Enabled = True
End If
End If
End Sub
添加到调用 UC 的父级的代码:
<uc1:ManageTitle id="ManageTitle"
IsReadOnly="True" runat="server">
</uc1:ManageTitle>
Is it possible for a user control to determine its "context" or its parent .aspx page in some way?
Right now I have a user control that is declared on a typical .aspx page as follows:
<%@ Register TagPrefix="uc1" TagName="ManageTitle" Src="../UserControls/ManageTitle.ascx" %>
The user control currently emits a textbox as follows:
<asp:textbox id="txtTitle" runat="server" MaxLength="60"
ToolTip="Describe the item with a short pithy title - most important keywords first"/>
The page_load for this .ascx file is currently like this:
Me.txtTitle.Text = SetPageTitle()
While some places in this web app need this (i.e. a textbox where end-user can type a "title"), I have other places where I want to show the "title" information in a "read-only" way. For example, rather than a textbox, I could use a label control or a textbox with Enabled="false" to prevent
data entry.
I suppose I could clone this small .ascx file and append a suffix to its name like _RO.ascx or something but I am wondering what the best approach would be.
In short, can a user control get some sort of "context" from the page that declares it or is there an altogether better way to accomplish this sort of thing? Thank you.
-- EDIT UPDATE WITH THE APPROACH SUGGESTED --------------------------
Code added to the UserControl:
Private mIsReadOnly As Boolean
Public Property IsReadOnly() As Boolean
Get
IsReadOnly = mIsReadOnly
End Get
Set(ByVal value As Boolean)
mIsReadOnly = value
End Set
End Property
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Page.IsPostBack Then
'Leave the textbox alone
Else
Me.txtTitle.Text = SetPageTitle() 'This is the original code
If IsReadOnly Then
Me.txtTitle.Enabled = False
Else
Me.txtTitle.Enabled = True
End If
End If
End Sub
Code added to the parent which invokes the UC:
<uc1:ManageTitle id="ManageTitle"
IsReadOnly="True" runat="server">
</uc1:ManageTitle>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您想遵循常见模式,请在 .ascx 控件(在代码隐藏中)上公开一个公共属性,该属性允许包含页面以编程方式设置其状态。您可以创建一个 State 属性,其值为可用状态的枚举(只读、可编辑等)
If you want to follow common patterns, expose a public property on your .ascx control (in the codebehind) that allows the containing page to set its state programmatically. You could create a State property whose value is an enum of available States (readonly, editable, etc)
这与之前的问题类似。我的答案有一个链接,显示如何完成此操作,以及更“标准”的替代方法
我可以在自定义控件中获取 Visual Studio.NET 中当前打开的 ASP 页面文件名吗?
This is similar to a previous question. My answer there has a link showing how this can be done, as well as an alternate approach that's more "standard"
Can I get the currently opened ASP page file name in Visual Studio.NET in a custom control?
是的,控件可以访问其上下文,例如使用
Me.Page
(我希望这是在VB.NET 中完成的方式)来访问页面。这种方法的问题在于,它使控件的可重用性降低,因为它们只能在它们知道的上下文中工作。也许切换依赖关系会更好,即页面/父控件应该调用控件的 GetPageTitle() 方法并使用返回值执行适当的操作。
Yes, a control can access its context, e.g. using
Me.Page
(I hope this is how it's done in VB.NET) to access the page.The problem with this approach is, that it makes your controls less reusable, because they will only work in contexts which they know. Probably it would be better to switch the dependency, i.e. the page/parent control should call a
GetPageTitle()
method of your control and do the appropriate things with the return value.