动态嵌套母版页、共享属性

发布于 2024-07-25 01:44:56 字数 358 浏览 1 评论 0原文

我有一个基本母版页,用于指定网站的主要布局模板。 它还处理一些根据部分更改选项卡的逻辑,并设置页面元信息。

我通过查看查询字符串、从数据库加载记录并根据该记录中找到的值动态设置嵌套母版页来动态加载嵌套母版页。 我需要加载动态嵌套母版页以实现布局和功能差异。

该记录中还有其他信息,我希望在基本母版页和动态加载的母版页中使用这些信息,这样我就可以避免额外的数据库调用。

目前,我已经设置了一个继承MasterPage的类作为基础母版页的基类。 我有一个共享(静态)属性,该属性保存代表我想要在基本母版页和嵌套的动态调用母版页之间共享的数据库调用的对象。

它有效,但看起来有点难看。 还有其他更好的解决方案吗?

I have a base master page that specifies the main layout template of a website. It also handles some logic that changes tabs depending on the section, and also sets page meta information.

I'm dynamically loading nested master pages by looking at the querystring, loading up a record from the database, and setting the nested master page dynamically based on a value found in that record. I need to load dynamic nested master pages for layout and functional differences.

There is additional information in that record that I want to use in the base master page and in the dynamically loaded master page so I can avoid additional database calls.

Currently, I have set up a class that inherits MasterPage to act as the base class for the base master page. I have a shared (static) property that holds the object representing the database call that I want to share between the base master page and the nested, dynamically called master page.

It works, but it seems a little ugly. Are there any other better solutions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

徒留西风 2024-08-01 01:44:56

您始终可以传递 HttpContext.Items 集合中的记录。 一旦它位于 Items 集合中,它就可供在请求期间到达 HttpContext 的所有事物使用。

You could always pass the record in the HttpContext.Items collection. Once it is in the Items collection it is available to every thing that can reach the HttpContext for the duration of the request.

骑趴 2024-08-01 01:44:56

好吧,我不得不在这个问题上睡一会,但我想出了一个更干净的解决方案。 我最终使用了页面的基类,而不是母版页的基类。 基本页面设置了我要在基本母版页中设置的元。

Public Class PageBase
    Inherits Page

    Private _DocDetails As FolderDocument
    Public Overridable ReadOnly Property DocDetails() As FolderDocument
        Get
            Return _DocDetails
        End Get
    End Property

    Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack() Then
            SetMeta()
        End If
    End Sub

    Protected Sub SetMeta()

        If DocDetails IsNot Nothing Then
            Page.Title = DocDetails.MetaTitle
            If DocDetails.MetaKeywords <> String.Empty Then
                Dim metaKeywords As New HtmlMeta()
                metaKeywords.Name = "Keywords"
                metaKeywords.Content = DocDetails.MetaKeywords
                Page.Header.Controls.Add(metaKeywords)
            End If
            If DocDetails.MetaDescription <> String.Empty Then
                Dim metaDescription As New HtmlMeta()
                metaDescription.Name = "Description"
                metaDescription.Content = DocDetails.MetaDescription
                Page.Header.Controls.Add(metaDescription)
            End If
        End If

    End Sub

End Class

..然后aspx页面继承这个基页并动态设置母版页。

<%@ Page Language="VB" Inherits="PageBase" %>
<script runat="server">

    Private _DocDetails As FolderDocument
    Public Overrides ReadOnly Property DocDetails() As FolderDocument
        Get
            Return _DocDetails
        End Get
    End Property

    Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As System.EventArgs)
        _DocDetails = FolderDocuments.GetFolderDocument()

        If _DocDetails IsNot Nothing Then
            If _DocDetails.MasterPage <> "" Then
                Me.MasterPageFile = String.Format("~/templates/{0}.master", _DocDetails.MasterPage)
            End If
        End If

    End Sub
</script>

...并且在动态调用的母版页中,我可以通过强制转换引用页面的基类:

Dim parentPage As PageBase = DirectCast(Page, PageBase)
Response.write(parentPage.DocDetails.Title)

Ok, I had to sleep on this one a bit, but I came up with a cleaner solution. I ended up using a base class for the page, instead of a base class for the master page. The base page sets the meta that I was going to set in the base master page.

Public Class PageBase
    Inherits Page

    Private _DocDetails As FolderDocument
    Public Overridable ReadOnly Property DocDetails() As FolderDocument
        Get
            Return _DocDetails
        End Get
    End Property

    Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack() Then
            SetMeta()
        End If
    End Sub

    Protected Sub SetMeta()

        If DocDetails IsNot Nothing Then
            Page.Title = DocDetails.MetaTitle
            If DocDetails.MetaKeywords <> String.Empty Then
                Dim metaKeywords As New HtmlMeta()
                metaKeywords.Name = "Keywords"
                metaKeywords.Content = DocDetails.MetaKeywords
                Page.Header.Controls.Add(metaKeywords)
            End If
            If DocDetails.MetaDescription <> String.Empty Then
                Dim metaDescription As New HtmlMeta()
                metaDescription.Name = "Description"
                metaDescription.Content = DocDetails.MetaDescription
                Page.Header.Controls.Add(metaDescription)
            End If
        End If

    End Sub

End Class

..And then the aspx page inherits this base page and dynamically sets the master page.

<%@ Page Language="VB" Inherits="PageBase" %>
<script runat="server">

    Private _DocDetails As FolderDocument
    Public Overrides ReadOnly Property DocDetails() As FolderDocument
        Get
            Return _DocDetails
        End Get
    End Property

    Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As System.EventArgs)
        _DocDetails = FolderDocuments.GetFolderDocument()

        If _DocDetails IsNot Nothing Then
            If _DocDetails.MasterPage <> "" Then
                Me.MasterPageFile = String.Format("~/templates/{0}.master", _DocDetails.MasterPage)
            End If
        End If

    End Sub
</script>

...and in the dynamically called master page I can reference the page's base class by casting:

Dim parentPage As PageBase = DirectCast(Page, PageBase)
Response.write(parentPage.DocDetails.Title)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文