2009-06-26 63 views
0

我有一個基本母版頁,它指定了網站的主要佈局模板。它還處理一些根據段落來改變標籤的邏輯,並且還設置頁面元信息。動態嵌套母版頁共享屬性

我通過查看查詢字符串,從數據庫加載記錄並基於該記錄中找到的值動態設置嵌套母版頁來動態加載嵌套母版頁。我需要爲佈局和功能差異加載動態嵌套母版頁。

我想在基本母版頁和動態加載母版頁中使用該記錄中的其他信息,以便我可以避免額外的數據庫調用。

目前,我已經設置了一個繼承MasterPage的類,以充當基礎母版頁的基類。我有一個共享(靜態)屬性,它保存代表我想在基本母版頁和嵌套的動態調用母版頁之間共享的數據庫調用的對象。

它的作品,但它似乎有點難看。還有其他更好的解決方案嗎?

回答

0

好吧,我不得不睡在這一個,但我想出了一個更清潔的解決方案。我最終爲頁面使用了基類,而不是母版頁的基類。基本頁面設置我將在基本母版頁中設置的元。

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) 
0

您可以隨時在HttpContext.Items集合中傳遞記錄。一旦它在Items集合中,它就可用於在請求期間可以到達HttpContext的所有東西。

+0

是的,這會工作,但據我所知,你失去了強類型(沒有一些額外的工作)。 – ScottE 2009-06-26 12:49:24