2012-03-08 59 views
2

我有masterat有runat =「server」&設置在body標籤上的Id。見下文背後將css類添加到body標籤c的反射#

<body id="MasterPageBodyTag" runat="server"> 

代碼對母版我已經添加以下代碼:

public HtmlGenericControl BodyTag 
{ 
    get { return MasterPageBodyTag; } 
    set { MasterPageBodyTag = value; } 
} 

現在我想CSS類在App_Code文件夾Class文件添加到身體標記。

在我的.aspx使用下面的代碼通過母版頁控制:

protected void Page_Load(object sender, EventArgs e) 
{ 
    backend.FindPage((PageTemp)this.Master); 

} 

現在上的Class1.cs我有以下

public static void FindPage(Control mp) 
{ 

    Page pg = (Page)HttpContext.Current.Handler; 


    PropertyInfo inf = mp.GetType().GetProperty("BodyTag");  

}

我想添加以下內容到找到BodyTag

//  BodyTag.Attributes.Add("class", "NewStyle"); 

但似乎無法找到添加atrribute或將inf投射到HtmlGenericControl的方法。

任何幫助將是偉大的。

+3

難道你不能用jQuery或類似的方法在客戶端實現嗎? – 2012-03-08 13:02:12

+1

這是否有用? http://www.codeproject.com/Articles/95438/Changing-A-Master-Page-Body-Tag-s-CSS-Class-for-Di – Rawling 2012-03-08 13:09:25

回答

4

而不是依賴於主頁類型,我只是使用FindControl來se Id爲body元素的拱形。假設body標籤是頂層母版頁上,還可以假設你可以使用嵌套母版頁,它看起來是這樣的:

private static MasterPage GetTopLevelMasterPage(Page page) 
{ 
    MasterPage result = page.Master; 
    if (page.Master == null) return null; 

    while(result.Master != null) 
    { 
     result = result.Master; 
    } 

    return result; 
} 

private static HtmlGenericControl FindBody(Page page) 
{ 
    MasterPage masterPage = GetTopLevelMasterPage(page); 
    if (masterPage == null) return null; 
    return masterPage.FindControl("MasterPageBodyTag") as HtmlGenericControl; 
} 

private void UpdateBodyCss() 
{ 
    HtmlGenericControl body = FindBody(this); 
    if(body != null) body.Attributes.Add(...); 
} 

你甚至可以通過搜索的消除對ID的依賴性HtmlGeneric標籤名稱爲「body」的控件:

private static HtmlGenericControl FindBody(Page page) 
{ 
    MasterPage masterPage = GetTopLevelMasterPage(page); 
    if (masterPage == null) return null; 
    foreach(Control c in masterPage.Controls) 
    { 
     HtmlGenericControl g = c as HtmlGenericControl; 
     if (g == null) continue; 
     if (g.TagName.Equals("body", StringComparison.OrdinalIgnoreCase)) return g; 
    } 
    return null; 
} 
+0

這對我有用。我嘗試使用解決方案@ http://www.codeproject.com/Articles/19386/Adding-attributes-to-the-lt-body-tag-when-using-Ma,但無法讓它工作。 – James 2013-11-13 21:09:02

+0

我用這個和標籤工作,但什麼可能是導致控件和ContentPlaceHolder形式的原因甚至不會加載頁面源? – hsobhy 2016-01-28 22:59:16

+0

是更specifec,我實際上使用了一個名爲_Culture與繼承System.Web.UI.Page的類中的解決方案,然後我在aspx頁面調用這個像公共類_Default Inherits _Culture – hsobhy 2016-01-28 23:04:19

0

你在做什麼似乎有點複雜,可能有更簡單的方法來解決這個問題。

要回答你的問題,你不需要使用反射。您可以簡單地將您傳遞給FindPage的參數轉換爲您創建的主頁類型。

您尚未指定您的母版頁的類型名稱,因此我將其命名爲MyMasterPage

所以FindPage應該是這樣的:

public static void FindPage(Control mp) 
{ 
    var masterPage = mp as MyMasterPage; 
    if (mp != null) 
    { 
     mp.BodyTag.Attributes.Add("class", "NewStyle"); 
    } 
} 
+0

我想用一個Id而不是依賴於FindControl MasterPage類型。 – Joe 2012-03-08 13:29:44

+0

我這樣做得到以下錯誤:對象引用未設置爲對象的實例。 – Chris 2012-03-08 14:13:27

+0

@Joe:一個更好的主意:-) – dariom 2012-03-08 23:02:00

1

你需要添加的aspx文件如下:

<%@ MasterType VirtualPath="~/Your_Master_Page.Master" %> 

,然後你可以在你的頁面的.cs做:

Master.BodyTag.Attributes.Add("class", "NewStyle"); 
相關問題