【问题标题】:Dynamic nested master pages, shared properties动态嵌套母版页,共享属性
【发布时间】:2010-11-06 00:46:54
【问题描述】:

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

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

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

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

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

【问题讨论】:

    标签: asp.net master-pages


    【解决方案1】:

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

    【讨论】:

    • 是的,这会起作用,但据我所知,你会失去强类型(无需额外工作)。
    • 我通常包装了需要使用静态属性类或实例类通过 HttpContext.Items 集合传递事物的情况。这意味着我会有类似 ContextItems 类的东西,它具有我放入项目中的数据的属性。我在那里输入以将数据从 Items 集合中返回为我输入的类型化类。
    【解决方案2】:

    好的,我不得不在这个上睡一会儿,但我想出了一个更清洁的解决方案。我最终使用页面的基类,而不是母版页的基类。基本页面设置了我要在基本主页中设置的元数据。

    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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-20
      • 2010-12-07
      • 1970-01-01
      相关资源
      最近更新 更多