一、 服务器端生成alert代码
message :消息内容
delay :延迟多长时间之后弹出消息框
FocusControl :关闭对话框之后焦点所在
AddScript :关闭对话框之后页面处罚的事件
IsUrl :关闭对话框之后转向的页面
Private
Sub alert_client()
Sub alert_client(ByVal message As String, ByVal delay As Integer, Optional ByVal FocusControl As Control = Nothing, Optional ByVal AddScript As String = Nothing, Optional 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
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()
Function confirm(ByVal message As String, ByVal PostButton As Button, Optional ByVal AddScript As String = Nothing) As 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
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()
Sub alert(ByVal message As String, ByVal FocusControl As Control, Optional ByVal delay As Integer = 10, Optional ByVal AddScript As String = Nothing, Optional ByVal IsUrl As Boolean = False)
alert_client(message, delay, FocusControl, AddScript, IsUrl)
End Sub
Protected Sub alert() Sub alert(ByVal message As String, Optional ByVal delay As Integer = 10, Optional ByVal AddScript As String = Nothing, Optional ByVal IsUrl As Boolean = False)
alert_client(message, delay, , AddScript, IsUrl)
End Sub
Protected Function UniqueMessageID() Function UniqueMessageID(Optional ByVal id As Integer = 1) As 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
alert_client(message, delay, FocusControl, AddScript, IsUrl)
End Sub
Protected Sub alert() Sub alert(ByVal message As String, Optional ByVal delay As Integer = 10, Optional ByVal AddScript As String = Nothing, Optional ByVal IsUrl As Boolean = False)
alert_client(message, delay, , AddScript, IsUrl)
End Sub
Protected Function UniqueMessageID() Function UniqueMessageID(Optional ByVal id As Integer = 1) As 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