Freelance Forums

Full Version: ASP.NET developement Tips: the basePage Class
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
it is very useful to add a pageBase class instead of the System.Web.UI.Page
it makes the size of your UI page down, and easy to manage your code........
improved the efficiency of development


---------------------------------code-------------------------------------

Option Explicit On

Imports System
Imports System.Web
Imports System.Web.UI
Imports System.ComponentModel
Imports System.Data
Imports Microsoft.VisualBasic
Namespace Web
Public Class PageBase
Inherits System.Web.UI.Page
Public Const OS_USERNAME As String = "WorkOnUser:"
Private Const OS_USERREALNAME As String = "WorkOnRealUser:"
Private Const OS_USERID As String = "WorkOnUserID:"
Private Const OS_USERROLE As String = "WorkOnUserRole:"
Public Const OS_MEMBERNAME As String = "member:"
Public Const OS_MEMBERID As String = "memberid:"
'get prefix of page URL,ine this example is localhost/WebFlow

Private Shared ReadOnly Property UrlSuffix() As String
Get
UrlSuffix = HttpContext.Current.Request.Url.Host + HttpContext.Current.Request.ApplicationPath
End Get
End Property
'generate the prefix of the pageURL,in this example is http://localhost/WebFlow
Public Shared ReadOnly Property UrlBase() As String
Get
UrlBase = "http://" & UrlSuffix
End Get
End Property
'save the users' login name in to Session
Public Property UserName() As String
Get
Try
UserName = CType(Session(OS_USERNAME), String)
Catch
UserName = Nothing
End Try
End Get
Set(ByVal Value As String)
If Value Is Nothing Then
Session.Remove(OS_USERNAME)
Else
Session(OS_USERNAME) = Value
End If
End Set
End Property
'save user's realname into Session
Public Property UserRealName() As String
Get
Try
UserRealName = CType(Session(OS_USERREALNAME), String)
Catch
UserRealName = Nothing
End Try
End Get
Set(ByVal Value As String)
If Value Is Nothing Then
Session.Remove(OS_USERREALNAME)
Else
Session.Item(OS_USERREALNAME) = Value
End If
End Set
End Property
'save userID into Session
Public Property UserID() As String
Get
Try
UserID = CType(Session(OS_USERID), String)
Catch
UserID = Nothing
End Try
End Get
Set(ByVal Value As String)
If Value Is Nothing Then
Session.Remove(OS_USERID)
Else
Session.Item(OS_USERID) = Value
End If
End Set
End Property
Public Property MemberName() As String
Get
Try
MemberName = CType(Session(OS_MEMBERNAME), String)
Catch
MemberName = Nothing
End Try
End Get
Set(ByVal Value As String)
If Value Is Nothing Then
Session.Remove(OS_MEMBERNAME)
Else
Session(OS_MEMBERNAME) = Value
End If
End Set
End Property
Public Property memberID() As String
Get
Try
memberID = CType(Session(OS_MEMBERID), String)
Catch
memberID = Nothing
End Try
End Get
Set(ByVal Value As String)
If Value Is Nothing Then
Session.Remove(OS_MEMBERID)
Else
Session.Item(OS_MEMBERID) = Value
End If
End Set
End Property


'when user logout, clear the session
Public Sub logout()
Try
Session.Remove(OS_USERID)

Session.Clear()
Response.Redirect("login.aspx")

Catch ex As Exception
End Try
End Sub
' filer the html code, this function is not complited
Public Property HtmDelFH(ByVal text As String) As String
Get
End Get
Set(ByVal Value As String)
Value = text
End Set
End Property

' send a message to user by a messagebox, this funtion usually is used to send the warning or error
' because messagebox is outrageous if it is used too much

' send a message

Public Function usermessage(ByVal message As String) As String
Dim script As String
script = "<script> alert('" + message + "')</script>"
Response.Write(script)
End Function

' send a message and direct to another page

Public Overridable Function usermessage(ByVal message As String, ByVal url As String) As String
Dim script As String
script = "<script> alert('" + message + "');" + " window.location.href = '" + url + "'" + "</script>"

Response.Write(script)

End Function


' delete some character if the length of the string is too long and break the page frame
' instead of the deleted chracter, "..." will be shown following the string

Public Function subStrings(ByVal data As DataSet, ByVal columnName As String, ByVal length As Integer) As DataSet
Dim objectRow As DataRow
Dim newdata As New DataSet
newdata = data.Copy
newdata.Tables(0).Clear()

Try

Catch ex As Exception
For Each objectRow In data.Tables(0).Rows
Dim a As String
a = objectRow.Item(columnName)
If a.Length >= length Then
objectRow.Item(columnName) = a.Substring(0, length - 1) + "..."
Else
objectRow.Item(columnName) = a
End If
newdata.Tables(0).Rows.Add(objectRow.ItemArray)
Next
subStrings = newdata
End Try

End Function
Public Function subStringsWithoutD(ByVal data As DataSet, ByVal columnName As String, ByVal length As Integer) As DataSet
Dim objectRow As DataRow
Dim newdata As New DataSet
newdata = data.Copy
newdata.Tables(0).Clear()

Try

Catch ex As Exception
For Each objectRow In data.Tables(0).Rows
Dim a As String
a = objectRow.Item(columnName)
If a.Length >= length Then
objectRow.Item(columnName) = a.Substring(0, length - 1)
Else
objectRow.Item(columnName) = a
End If
newdata.Tables(0).Rows.Add(objectRow.ItemArray)
Next
subStringsWithoutD = newdata
End Try

End Function

' used to check user's authority

Public Function checkUserAuthority(ByVal userID As Integer, ByVal PermissionID As Integer) As Boolean
Dim data As DataSet

With New businessRules.oversea.businessRule.userSystem
data = .checkUserAuthority(("pr_accounts_getPermissionsByUserID"), userID, PermissionID)
End With
If Not data Is Nothing Then
If data.Tables(0).Rows.Count > 0 Then
If data.Tables(0).Rows(0).Item(0) > 0 Then
checkUserAuthority = True
Else
checkUserAuthority = False
End If
End If
Else

End If
End Function



End Class


End Namespace
Is there still much use for the basepage class now that 2.0 has introduced Master Pages? It seems to me the master page makes the base page class obsolete.
Reference URL's