⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 作项目时用的方法何函数.txt

📁 学习积累的代码段
💻 TXT
📖 第 1 页 / 共 2 页
字号:
   
EagleFly
飞翔在天宇中的鹰,永高飞于天空 
博客园    首页    新随笔    联系    聚合    管理 随笔 - 3  文章 - 18  trackbacks - 0 
< 2005年9月 > 
 
日 一 二 三 四 五 六 
28 29 30 31 1 2 3 
4 5 6 7 8 9 10 
11 12 13 14 15 16 17 
18 19 20 21 22 23 24 
25 26 27 28 29 30 1 
2 3 4 5 6 7 8 

留言簿给我留言 
查看公开留言 
查看私人留言 
我参与的团队大连.NET俱乐部(0/347) 
最新随笔1. 平时在做ASP.NET项目里经常使用的一些函数和方法 
2. 用CodeSmith生成数据库实体类的代码 
3. 发布一个用IronPython写的小的代码编辑器 
随笔档案2005年9月 (3) 
文章分类ASP.NET 
C# 
IronPython(1) 
Other 
VB.Net 
收藏夹df 
搜索
  
最新评论 1. re: 发布一个用IronPython写的小的代码编辑器 
IronPython 现在已经发布了 
--银河 
2. re: 平时在做ASP.NET项目里经常使用的一些函数和方法 
收藏,有些是很有用的嘛,谢谢楼主 
--下楼弹吉他 
3. re: 平时在做ASP.NET项目里经常使用的一些函数和方法 
不好意思打擊你,基本很垃圾 
--拉拉 
4. re: 平时在做ASP.NET项目里经常使用的一些函数和方法 
很不错,我用了一下,速度很快,如果能够在DataGrid上编辑就好了! 
--天梦 
5. re: 平时在做ASP.NET项目里经常使用的一些函数和方法 
有沒有C#代碼呀 
--louis 
平时在做ASP.NET项目里经常使用的一些函数和方法 
这是我平时在做ASP.NET项目里经常使用的一些函数和方法,把它们合到一个类中,希望对你们有用.

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

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -