如何使用母版页在基类中分配主题?

时间:2009-09-30 01:57:48

标签: asp.net class themes base

我正在尝试根据浏览器类型分配主题。我想在基类中这样做,所以它只需要在一个地方(我使用的是母版页)。我编写了以下代码,但这里的“OnLoad”是在“Page_PreInit”之前执行的。这需要进入Page_PreInit,但为什么不解雇呢?

Imports Microsoft.VisualBasic

Public Class MyBaseClass
Inherits System.Web.UI.Page

Private Sub Page_PreInit(ByVal sender As Object, ByVal e As System.EventArgs)
    'Assign the CSS Theme based on the Browser Type
    If (Request.Browser.Type = "IE8") Then
        Page.Theme = "Standard-IE8"
    Else
        Page.Theme = "Standard"
    End If
End Sub

Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)

    MyBase.OnLoad(e)
End Sub

End Class

然后,我将我的登录页面编码为继承基类:

Partial Class Login
'Inherits System.Web.UI.Page
Inherits MyBaseClass

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

谢谢你, 詹姆斯

1 个答案:

答案 0 :(得分:1)

您需要在基类中覆盖 OnPreInit

Protected Overrides Sub OnPreInit(ByVal e As System.EventArgs)
        'Assign the CSS Theme based on the Browser Type
        If (Request.Browser.Type = "IE8") Then
            Page.Theme = "Standard-IE8"
        Else
            Page.Theme = "Standard"
        End If
        MyBase.OnPreInit(e)
    End Sub

有关使用自定义基类的详细信息,请参阅here