Imports Microsoft.VisualBasic
Imports System.Data
Imports System.Data.SqlClient
Imports System.Text
Imports System.Collections
Imports System.Runtime.CompilerServices
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
'Imports General
'Imports General.Convertor
Namespace System.Web.UI.Extensions
Public Module PageExtension
#Region "Utility Section"
''' <summary>
''' Trace path from page for any javascript reference location
''' </summary>
''' <param name="Am"></param>
''' <param name="argPathFileLocation"></param>
''' <returns></returns>
''' <remarks></remarks>
<Extension()> _
Public Function TraceIncludeBasePath(ByVal Am As Page, ByVal argPathFileLocation As String) As String
Dim strRet As String = argPathFileLocation
Dim CurrentURLStr As String = Am.Server.MapPath(Am.Request.ServerVariables("URL"))
Dim ScriptURL As String = Am.Server.MapPath(argPathFileLocation)
Dim tempStr As String = String.Empty
Dim founded As Boolean = False
For i As Integer = 1 To ScriptURL.Length - 1
tempStr = ScriptURL.Substring(0, i)
If (Not tempStr.Equals(CurrentURLStr.Substring(0, i))) Then
founded = True
CurrentURLStr = CurrentURLStr.Substring(i - 1)
ScriptURL = ScriptURL.Substring(i - 1)
Exit For
End If
Next
If (Not founded) Then Return String.Empty
'After pass mappath path separator will be "\"
Dim ixStr As String() = CurrentURLStr.Split(New Char() {"\"c}, StringSplitOptions.RemoveEmptyEntries)
Dim totalTrace As Integer = ixStr.Length - 1
'Replace to valid path separator
strRet = ScriptURL.Replace("\"c, "/"c)
For i As Integer = 0 To totalTrace - 1
strRet = "../" + strRet
Next
Return strRet.ToUpper()
End Function
''' <summary>
''' Adding any script file to page suggest to use when uses sub directory for any page
''' </summary>
''' <param name="Am"></param>
''' <param name="argScriptFileLocation"></param>
''' <remarks></remarks>
<Extension()> _
Public Sub HeaderAddNewScriptFile(ByVal Am As Page, ByVal argScriptFileLocation As String)
Dim newScriptParser As New HtmlGenericControl("script")
newScriptParser.Attributes.Add("src", Am.TraceIncludeBasePath(argScriptFileLocation))
newScriptParser.Attributes.Add("type", "text/javascript")
Am.Header.Controls.Add(New LiteralControl(vbTab))
Am.Header.Controls.Add(newScriptParser)
Am.Header.Controls.Add(New LiteralControl(Environment.NewLine))
End Sub
End Module
End Namespace
การใช้งาน Code (VB.NET)
....
Imports System.Web.UI.Extensions
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Me.HeaderAddNewScriptFile("~/Scripts/jquery-1.4.2.min.js")
Me.HeaderAddNewScriptFile("~/Scripts/jquery-ui-1.8.1.custom.min.js")
If (Not Page.IsPostBack) Then
End If
End Sub
Dim sb As New StringBuilder
sb.Append("<script type='text/javascript' language='javascript'>")
sb.Append("alert('usernameหรือpasswordไม่ถูกต้อง');")
sb.Append("document.location ='login.aspx';")
sb.Append("</script>")
Page.ClientScript.RegisterStartupScript(Page.GetType, "RedirectScript", sb.ToString)