Wednesday, August 8, 2007

Solution of MsgBox in ASP.net



You can't use MessageBox
in ASP.NET. You don't have any graphical user

interface, so there is nowhere that you can display the messagebox.



If you were allowed to use a messagebox in ASP.NET, it would pop up on

the web server where noone would see it, and IIS
would hang indefinitely

waiting for someone to click it.





Remember that this code is running on
the web server in a process that is not running as the currently logged on
(to the server) user. Therefore the process will be running under its own
Window Station, and will not be able to interact with the desktop in any way.
Consequently, you cannot display a MessageBox on the
server without setting the flags to indicate that you want to display it on the
currently logged in user's Window Station (effectively the
default desktop).



Typically, displaying message
boxes on servers is regarded as a very bad thing, as a lot of servers are
locked in computer rooms and there is no user to dismiss them. Therefore, are
you really sure that you want to do this, or would you really prefer to display
a message box on the browser (using client-side JavaScript)?





Here I
am showing another way to display the MsgBox in page.





This
shows an alert on page_load (or else).

No need to create an instance of the class.

Example:


Protected Sub Page_Load(ByVal
sender As Object, ByVal e As System.EventArgs)
Handles Me.Load

cmsgbox.Client_Alert_OnLoad("Hello
world")

End Sub



Note that you won't need to 'format' special characters like \ and " etc..






Public Shared Sub Client_Alert_OnLoad(ByVal sMessage As String,
Optional ByVal sURL As
String = "")

On Error Resume Next

Dim str As String

Dim P As System.Web.UI.Page
= CType(System.Web.HttpContext.Current.Handler,
System.Web.UI.Page)

Dim sb As New StringBuilder(Len(sMessage) * 5)

sMessage = sMessage.Replace(Chr(0),
"")

For Each c As String In sMessage
: sb.Append("\x" & Right("0"
& Hex(Asc(c)), 2)) : Next

str = vbCrLf & "<script language=javascript>"
& vbCrLf

str = str & " alert('" & sb.ToString &amp; "');" & vbCrLf


If Len(sURL) > 0
Then str = str & "
window.location='" & sURL &amp; "';" & vbCrLf


str = str & "</script>" & vbCrLf

P.ClientScript.RegisterStartupScript(P.GetType, "", str)

End Sub















Example:

cmsgbox.Client_Confirm_OnButton(Me.Button1, Chr(34)
& "hello world \/-=" & Chr(34)
& vbCrLf & "ok to format c:?")





Public Shared Sub Client_Confirm_OnButton(ByRef oButton As System.Web.UI.WebControls.Button, ByVal
sMessage As String)

On Error Resume Next

Dim str
As String

Dim sFunction
As String = ("msgbox_confirm_" & oButton.ClientID).Replace("$", "_")

Dim sb
As New StringBuilder(Len(sMessage)
* 5)

sMessage
= sMessage.Replace(Chr(0),
"")

For Each c As String In sMessage : sb.Append("\x"
& Right("0" & Hex(Asc(c)), 2)) :
Next

str
= vbCrLf & "<script language=javascript>" & vbCrLf


str
= str & "" & vbCrLf


str
= str & "function " & sFunction &amp; "()" & vbCrLf


str = str
& " if ( !window.confirm('"
& sb.ToString &amp; "') ) {"

str
= str & " window.event.returnValue
= false;" & vbCrLf

str
= str & " return 0;" & vbCrLf

str
= str & " }" & vbCrLf

str
= str & "}" & vbCrLf


str
= str & "</script>" & vbCrLf

oButton.OnClientClick
= "javascript:" & sFunction
&amp; "();"

oButton.Page.ClientScript.RegisterStartupScript(oButton.Page.GetType, sFunction, str)

End Sub