.net 服务器端生成alert/confim 消息

来源:互联网 发布:qq群淘宝客优惠劵软件 编辑:程序博客网 时间:2024/04/30 02:22

一、 服务器端生成alert代码

message            :消息内容

delay                    :延迟多长时间之后弹出消息框

FocusControl    :关闭对话框之后焦点所在

AddScript           :关闭对话框之后页面处罚的事件

IsUrl                   :关闭对话框之后转向的页面

 

 Private Sub alert_client(ByVal message As StringByVal delay As IntegerOptional ByVal FocusControl As Control = NothingOptional ByVal AddScript As String = NothingOptional ByVal IsUrl As Boolean = False)
            
Dim s As String, msg As String, MsgID As String
            
Dim sb As New System.Text.StringBuilder(message)
            sb.Replace(vbCrLf, 
" ")
            sb.Replace(
"'""'")
            sb.Replace(
"""""""")

            msg 
= sb.ToString()
            MsgID 
= UniqueMessageID()

            s 
= "<script language='javascript'>" & vbCrLf
            s 
+= "function " & MsgID & "()" & vbCrLf
            s 
+= "{" & vbCrLf
            s 
+= "alert(""" & msg & """);" & vbCrLf
            
If Not FocusControl Is Nothing Then
                s 
+= "window.document.all['" & FocusControl.ClientID.ToString() & "'].focus();" & vbCrLf
                
If String.Compare(FocusControl.GetType.ToString, "System.Web.UI.WebControls.LinkButton"<> 0 Then
                    s 
+= "window.document.all['" & FocusControl.ClientID.ToString() & "'].select();" & vbCrLf
                
End If
            
End If
            
If Not AddScript Is Nothing Then
                
If IsUrl Then
                    s 
+= "window.location='" & AddScript & "';" & vbCrLf
                
Else
                    s 
+= AddScript & vbCrLf
                
End If
            
End If
            s 
+= "}" & vbCrLf
            s 
+= "//-->" & vbCrLf
            
If delay = 0 Then
                s 
+= (MsgID & "();")
            
Else
                s 
+= "window.setTimeout(" & MsgID & "," & delay & ");" & vbCrLf
            
End If

            s 
+= "</script>" & vbCrLf

            
Me.Page.ClientScript.RegisterClientScriptBlock(Me.GetType(), MsgID, s)
        
End Sub

 

二、服务器生成confim 

message            :消息内容

PostButton         :关闭对话框之后触发的页面postback事件

AddScript           :关闭对话框之后页面处罚的事件

 Protected Function confirm(ByVal message As StringByVal PostButton As Button, Optional ByVal AddScript As String = NothingAs Boolean
            
Dim s As String, msg As String, MsgID As String
            
Dim sb As New System.Text.StringBuilder(message)
            sb.Replace(vbCrLf, 
" ")
            sb.Replace(
"'""'")
            sb.Replace(
"""""""")

            msg 
= sb.ToString()
            MsgID 
= UniqueMessageID()

            s 
= "<script language='javascript'>" & vbCrLf
            s 
+= "<!--" & vbCrLf
            s 
+= "/*  Client Alert Function" & vbCrLf
            s 
+= "    CopyRight APJCorp.com" & vbCrLf
            s 
+= "    Author:bss (Bianshusen@apjcorp.com)" & vbCrLf
            s 
+= "    Date:  2004-8-24" & vbCrLf
            s 
+= "*/" & vbCrLf
            s 
+= "function " & MsgID & "()" & vbCrLf
            s 
+= "{" & vbCrLf
            s 
+= "var b;" & vbCrLf
            s 
+= "b=false;" & vbCrLf
            s 
+= "b=confirm(""" & msg & """);" & vbCrLf
            s 
+= "if (b==false)" & vbCrLf
            s 
+= "{" & vbCrLf
            s 
+= " return false;" & vbCrLf
            s 
+= "}" & vbCrLf
            s 
+= "else" & vbCrLf
            s 
+= "{" & vbCrLf
            s 
+= " __doPostBack('" & PostButton.UniqueID & "','') ;" & vbCrLf
            s 
+= "}" & vbCrLf

            
If Not AddScript Is Nothing Then
                s 
+= AddScript & vbCrLf
            
End If
            s 
+= "}" & vbCrLf
            s 
+= "//-->" & vbCrLf


            s 
+= "window.setTimeout(" & MsgID & ",10);" & vbCrLf

            s 
+= "</script>" & vbCrLf

            
'Me.Page.RegisterClientScriptBlock(MsgID, s)

            
Me.Page.ClientScript.RegisterClientScriptBlock(Me.GetType(), MsgID, s)

            
Return True
        
End Function

 

相关代码:

 

   Protected Sub alert(ByVal message As StringByVal FocusControl As Control, Optional ByVal delay As Integer = 10Optional ByVal AddScript As String = NothingOptional ByVal IsUrl As Boolean = False)
            alert_client(message, delay, FocusControl, AddScript, IsUrl)
        
End Sub

        
Protected Sub alert(ByVal message As StringOptional ByVal delay As Integer = 10Optional ByVal AddScript As String = NothingOptional ByVal IsUrl As Boolean = False)
            alert_client(message, delay, , AddScript, IsUrl)
        
End Sub


        
Protected Function UniqueMessageID(Optional ByVal id As Integer = 1As String
            
Const MessageName As String = "__MessageBox"

            
If Not Me.Page.ClientScript.IsClientScriptBlockRegistered(MessageName & id.ToString) Then

                
Return MessageName & id.ToString
            
Else
                
Return UniqueMessageID(id + 1)
            
End If
        
End Function
原创粉丝点击