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

一、 服务器端生成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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值