CSDN博客

img aliang128

在VB代码中调用JavaScript ClientSidePage 代码实例

发表于2004/9/30 9:35:00  1743人阅读

分类: ASP.NET

Public Class ClientSidePage
    Inherits System.Web.UI.Page

    Public Sub DisplayAlert(ByVal message As String)
        RegisterClientScriptBlock(Guid.NewGuid().ToString(), _
                 "<script language=""JavaScript"">" & GetAlertScript(message) & "</script>")
    End Sub

    Public Function GetAlertScript(ByVal message As String) As String
        Return "alert('" & message.Replace("'", "/'") & "');"
    End Function

    Public Sub GiveFocus(ByVal c As Control)
        RegisterClientScriptBlock("CSP-focus-function", _
                    "<script language=""JavaScript"">" & vbCrLf & _
                    "function CSP_focus(id) {" & _
                    "  var o = document.getElementById(id); if (o != null) o.focus(); " & _
                    "}" & vbCrLf & _
                    "</script>")

        RegisterStartupScript("CSP-focus", "<script language=""JavaScript"">CSP_focus('" & c.ClientID & "');</script>")
    End Sub

    Public Sub DisplayPopup(ByVal url As String, ByVal options As String)
        RegisterStartupScript(Guid.NewGuid().ToString(), _
                "<script language=""JavaScript"">" & GetPopupScript(url, options) & "</script>")
    End Sub

    Public Function GetPopupScript(ByVal url As String, ByVal options As String) As String
        Return "var w = window.open(""" & _
                         url & """, null, """ & options & """);"
    End Function

    Public Sub CloseWindow(Optional ByVal refreshParent As Boolean = False)
        RegisterClientScriptBlock("CSP-close-popup", "<script language=""JavaScript"">" & GetCloseWindowScript(refreshParent) & "</script>")
    End Sub

    Public Function GetCloseWindowScript(Optional ByVal refreshParent As Boolean = False) As String
        Dim script As String
        If refreshParent Then
            script &= "window.opener.location.reload();"
        End If

        Return "self.close();"
    End Function

    Protected Sub RegisterOnchangeScript()
        If Not IsClientScriptBlockRegistered("CSP-onchange-function") Then
            RegisterClientScriptBlock("CSP-onchange-function", "<script language=""JavaScript"">" & _
                     "var isDirty= false;" & vbCrLf & _
                     "function CSP_checkForChange(msg) {" & vbCrLf & _
                     "  if (isDirty) return confirm(msg); else return true;" & vbCrLf & _
                     "}" & vbCrLf & _
                  "</script>")
        End If
    End Sub

    Public Sub MonitorChanges(ByVal c As WebControl)
        RegisterOnchangeScript()
        If TypeOf c Is CheckBox Or TypeOf c Is CheckBoxList Or TypeOf c Is RadioButtonList Then
            c.Attributes("onclick") = "isDirty = true;"
        Else
            c.Attributes("onchange") = "isDirty = true;"
        End If
    End Sub

    Public Sub ConfirmOnExit(ByVal c As WebControl, ByVal message As String)
        RegisterOnchangeScript()
        c.Attributes("onclick") = "return CSP_checkForChange('" & message.Replace("'", "/'") & "');"
    End Sub

    Public Sub ConfirmOnChange(ByVal ddl As DropDownList, ByVal message As String)
        'Register the script block
        If Not IsStartupScriptRegistered("CSP-ddl-onchange") Then
            RegisterStartupScript("CSP-ddl-onchange", _
                "<script language=""JavaScript"">" & _
                "var CSP_savedDDLID = document.getElementById('" & ddl.ClientID & "').value;" & vbCrLf & _
                "function resetDDLIndex() {" & vbCrLf & _
                "   document.getElementById('" & ddl.ClientID & "').value = CSP_savedDDLID;" & vbCrLf & _
                "}" & vbCrLf & _
                "</script>")
        End If

        ddl.Attributes("onchange") = "if (!confirm('" & message.Replace("'", "/'") & "')) resetDDLIndex(); else "
    End Sub
End Class

阅读全文
0 0

相关文章推荐

img
取 消
img