PageBase撰寫
首先在專案上按滑鼠右鍵→加入新項目→選擇類別→名稱為【PageBase.vb】,
接著如果您沒有開啟個App_Code的資料夾,系統會問是否要新增App_Code資料夾,
並將該類別的程式內容放入該資料夾中,此時選是,會在App_Code中增加一個PageBase.vb的程式,
並且程式內容為一個空的Class
接著,把剛剛CodeFile要繼承System.Web.UI.Page的那行指令複製到PageBase中如下
Imports Microsoft.VisualBasic
Public Class PageBase
Inherits System.Web.UI.Page
End Class
此時就能夠發現該Class有了Page的相關事件可以選用
接著就來寫一下測試的Function,Property來測試看看,我們寫了個SayHi的Funcation,以及一個屬性來測試一下,原始碼如下
Imports Microsoft.VisualBasic
Public Class PageBase
Inherits System.Web.UI.Page
Public Function SayHello(ByVal InStr As String) As String
Return "Hello," + InStr
End Function
Dim _BrwP As Boolean = False
Public Property BrwP() As Boolean '瀏覽屬性
Get
Return _BrwP
End Get
Set(ByVal value As Boolean)
_BrwP = value
End Set
End Property
Public Sub ChkBrwP(ByVal UID As String)
If UID = "topcat" Then
Me.BrwP = True
Else
Me.BrwP = False
End If
End Sub
Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Response.Write("PageBase Load.
")
End Sub
End Class
寫完後請記得儲存一下,其他畫面才能夠直接使用剛剛撰寫在PageBase中的程式與屬性
再來新增一個aspx來測試一下,相關內容如下:
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
http://www.w3.org/1999/xhtml" >
CodeFile:default2.aspx.vb
原本繼承System.Web.UI.Page的地方,現在改成繼承剛剛的PageBase
Partial Class Default2
Inherits PageBase
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim UID As String = Request.QueryString("UID")
Dim Rc As String = Me.SayHello(UID)
Response.Write("Default2.aspx Load.
")
Response.Write(Rc & "
")
Me.ChkBrwP(UID)
Response.Write(Me.BrwP.ToString)
End Sub
Protected Sub btn2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn2.Click
Response.Redirect("default2.aspx?UID=John")
End Sub
Protected Sub btn1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn1.Click
Response.Redirect("default2.aspx?UID=topcat")
End Sub
End Class
====另一個sample=====
public class BasePage : System.Web.UI.Page
{
public BasePage():base()
{
}
protected override void OnPreLoad(EventArgs e)
{
base.OnPreLoad(e);
if (this.ValidatePermission() == false)
{
throw new Exception("滾吧!!你還沒有獲得這個頁面的授權!!");
}
}
protected virtual bool ValidatePermission()
{
return false;
}
}
留言列表