2008-10-23 66 views
3

我試圖幫助另一個程序員使用帶有代碼段的Default.aspx頁面,但不幸的是我有點遺憾。ASP.NET 2.0中的Page.Load問題

Partial Class _Default 
Inherits OverheadClass 
'A bunch of global variables here' 

Private Sub page_load(ByVal sender As Object, ByVal e As System.Eventarts) Handles Me.Load 
'Function goes here' 

而在OverheadClass我們

Public Sub Sub_OverheadClass_Load(ByVal sender As Object, ByVal e as System.EventArgs) Handles MyClass.Load 

的OverheadClass繼承時,預期的效果是,我們需要它的負荷,在網頁上運行的負載事件之前運行。這可能是一個非常簡單的答案,我錯過了。

編輯:我忘了注意我們用VB寫的,而不是C#,因爲你們很多人習慣ASP。

回答

10

你應該能夠覆蓋在onload和第一調用基類的OnLoad,然後你的類,例如:

C#版本

protected override void OnLoad(EventArgs e) 
{ 
    base.OnLoad(e); 

    // Do some stuff here 
} 

VB版

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

    MyBase.OnLoad(e) 

    ' Do some stuff here 

End Sub 
+0

這是絕對正確的答案,因爲_Default.page_load不會覆蓋OverheadClass.Sub_OverheadClass_Load。另一種選擇可能是讓OverheadClass.Sub_OverheadClass_Load重新提升事件,但我認爲這會導致NASTY副作用。 – 2008-10-23 15:37:41

3

在VB它將是:

Private Sub page_load(ByVal sender As Object, ByVal e As System.Eventarts) Handles Me.Load 
    Mybase.Sub_OverheadClass_Load(e) 
End Sub 
0

您的默認頁面應該繼承OverheadClass

Partial Public Class _Default 
     Inherits OverheadClass 

     Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 
      'Do some page stuff' 
     End Sub 
    End Class 

而且OverheadClass應該繼承System.Web.UI.Page

Public Class OverheadClass 
    Inherits System.Web.UI.Page 
    Public Sub Sub_OverheadClass_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyClass.Load 
     'Do some base stuff' 
    End Sub 
End Class 
0
Partial Class OverheadClass 
    Inherits System.Web.UI.Page 

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



Partial Class _Default 
    Inherits OverheadClass 

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