在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
Imports System.Web
Imports System.Web.UI Imports System.Web.UI.HtmlControls Imports System.Web.UI.WebControls Namespace Webs Public Class WebUtils Private Shared m_sScriptPath As String Public Sub SetFormFocus(ByVal control As Control) If Not control.Page Is Nothing And control.Visible Then If control.Page.Request.Browser.JavaScript = True Then ' Create JavaScript Dim sb As New System.Text.StringBuilder sb.Append("<SCRIPT LANGUAGE='JavaScript'>") sb.Append("<!--") sb.Append(ControlChars.Lf) sb.Append("function SetInitialFocus() {") sb.Append(ControlChars.Lf) sb.Append(" document.") ' Find the Form Dim objParent As Control = control.Parent While Not TypeOf objParent Is System.Web.UI.HtmlControls.HtmlForm objParent = objParent.Parent End While sb.Append(objParent.ClientID) sb.Append("['") sb.Append(control.UniqueID) sb.Append("'].focus(); }") sb.Append("window.onload = SetInitialFocus;") sb.Append(ControlChars.Lf) sb.Append("// -->") sb.Append(ControlChars.Lf) sb.Append("</SCRIPT>") ' Register Client Script control.Page.RegisterClientScriptBlock("InitialFocus", sb.ToString()) End If End If End Sub Public Shared Function GetSelectedString(ByVal ddl As System.Web.UI.WebControls.ListControl, Optional ByVal ExcludeFirstSelection As Boolean = False) As String Dim leastSelection As Int32 = 0 If ddl.SelectedIndex < leastSelection Then Return "" Else Return ddl.SelectedItem.Value End If End Function Public Shared Function GetSelectedInt(ByVal ddl As System.Web.UI.WebControls.ListControl, Optional ByVal ExcludeFirstSelection As Boolean = False) As Int32 Dim str As String = GetSelectedString(ddl, ExcludeFirstSelection) Return General.Utils.ParseInt(str) End Function Public Shared Sub SetSelectedValue(ByVal ddl As ListControl, ByVal value As Object) Dim index As Int32 = ddl.Items.IndexOf(ddl.Items.FindByValue(value.ToString())) If index >= 0 Then ddl.SelectedIndex = index Else ddl.SelectedIndex = 0 End If End Sub Public Shared Sub PostBackToNewWindow(ByVal control As WebControl) control.Attributes.Add("onclick", "javascript:document.forms(0).target='_new';" + control.Page.GetPostBackEventReference(control) + ";document.forms(0).target='_self';return false") End Sub Public Shared Sub BindDropdownWithDefault(ByVal ddl As ListControl, ByVal datasource As Object) ddl.DataSource = datasource ddl.DataBind() ddl.Items.Insert(0, "") ddl.SelectedIndex = 0 End Sub Public Shared Function AddPage(ByVal path As String, ByVal pageName As String) As String Dim friendlyPath As String = path If (friendlyPath.EndsWith("/")) Then friendlyPath = friendlyPath & pageName Else friendlyPath = friendlyPath & "/" & pageName End If Return friendlyPath End Function ''' ----------------------------------------------------------------------------- ''' <summary> ''' Searches control hierarchy from top down to find a control matching the passed in name ''' </summary> ''' <param name="objParent">Root control to begin searching</param> ''' <param name="strControlName">Name of control to look for</param> ''' <returns></returns> ''' <remarks> ''' This differs from FindControlRecursive in that it looks down the control hierarchy, whereas, the ''' FindControlRecursive starts at the passed in control and walks the tree up. Therefore, this function is ''' more a expensive task. ''' </remarks> ''' ----------------------------------------------------------------------------- Public Shared Function FindControlRecursive(ByVal objParent As Control, ByVal strControlName As String) As Control Dim objCtl As Control Dim objChild As Control objCtl = objParent.FindControl(strControlName) If objCtl Is Nothing Then For Each objChild In objParent.Controls If objChild.HasControls Then objCtl = FindControlRecursive(objChild, strControlName) If Not objCtl Is Nothing Then Exit For Next End If Return objCtl End Function Public Shared Function GetAttribute(ByVal objControl As Control, ByVal strAttr As String) As String Select Case True Case TypeOf objControl Is WebControl Return CType(objControl, WebControl).Attributes(strAttr) Case TypeOf objControl Is HtmlControl Return CType(objControl, HtmlControl).Attributes(strAttr) Case Else 'throw error? End Select End Function Public Shared Sub SetAttribute(ByVal objControl As Control, ByVal strAttr As String, ByVal strValue As String) Dim strOrigVal As String = GetAttribute(objControl, strAttr) If Len(strOrigVal) > 0 Then strValue = strOrigVal & strValue Select Case True Case TypeOf objControl Is WebControl Dim objCtl As WebControl = CType(objControl, WebControl) If objCtl.Attributes(strAttr) Is Nothing Then objCtl.Attributes.Add(strAttr, strValue) Else objCtl.Attributes(strAttr) = strValue End If Case TypeOf objControl Is HtmlControl Dim objCtl As HtmlControl = CType(objControl, HtmlControl) If objCtl.Attributes(strAttr) Is Nothing Then objCtl.Attributes.Add(strAttr, strValue) Else objCtl.Attributes(strAttr) = strValue End If Case Else 'throw error? End Select End Sub Public Shared Sub AddButtonConfirm(ByVal objButton As WebControl, ByVal strText As String) objButton.Attributes.Add("onClick", "javascript:return confirm('" & GetSafeJSString(strText) & "');") End Sub Public Shared Function GetSafeJSString(ByVal strString As String) As String If Len(strString) > 0 Then Return System.Text.RegularExpressions.Regex.Replace(strString, "(['""])", "\$1") Else Return strString End If End Function Public Shared Property ScriptPath() As String Get If Len(m_sScriptPath) > 0 Then Return m_sScriptPath ElseIf Not System.Web.HttpContext.Current Is Nothing Then If System.Web.HttpContext.Current.Request.ApplicationPath.EndsWith("/") Then Return System.Web.HttpContext.Current.Request.ApplicationPath & "js/" Else Return System.Web.HttpContext.Current.Request.ApplicationPath & "/js/" End If End If End Get Set(ByVal Value As String) m_sScriptPath = Value End Set End Property Public Shared Sub FocusControlOnPageLoad(ByVal ControlID As String, ByVal FormPage As System.Web.UI.Page) Dim JSStr As String JSStr = "<script>" & vbCrLf JSStr &= "function ScrollView() {" & vbCrLf JSStr &= "var el = document.getElementById('" & ControlID & "');" & vbCrLf JSStr &= "if (el != null) {" & vbCrLf JSStr &= "el.scrollIntoView();" & vbCrLf JSStr &= "el.focus();" & vbCrLf JSStr &= "}" & vbCrLf & "}" & vbCrLf JSStr &= "window.onload = ScrollView;" & vbCrLf JSStr &= " </script>" & vbCrLf FormPage.RegisterClientScriptBlock("CtrlFocus", JSStr) End Sub '得到操作系统和游览器信息 Public Shared Function GetBrowserInfo(ByVal AgentStr As String, ByVal Style As Integer) As String Dim GetInfo As String GetInfo = "" Select Case Style Case 1 '得到操作系统 If (InStr(AgentStr, "NT 5.1") > 0) Then GetInfo = "操作系统:Windows XP" ElseIf (InStr(AgentStr, "Tel") > 0) Then GetInfo = "操作系统:Telport" ElseIf (InStr(AgentStr, "webzip") > 0) Then GetInfo = "操作系统:webzip" ElseIf (InStr(AgentStr, "flashget") > 0) Then GetInfo = "操作系统:flashget" ElseIf (InStr(AgentStr, "offline") > 0 全部评论
专题导读
热门推荐
热门话题
阅读排行榜
|
请发表评论