Category Archives: VB.Net

Show Message Box in

It’s not possible to show an alert message box in using server side code (like windows apps, msgbox).

But this can be achieved by generating a client side Java Script code in a server-side event.


Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
showMsg(“hey there”, Me)
End Sub

Public Sub showMsg(ByVal sMsg As String, ByVal frm As Web.UI.Page)

Dim sb As New System.Text.StringBuilder
Dim oFormObject As System.Web.UI.Control

sMsg = sMsg.Replace(“‘”, “\'”)
sMsg = sMsg.Replace(Chr(34), “\” & Chr(34))
sMsg = sMsg.Replace(vbCrLf, “\n”)
sMsg = “<script language=javascript>alert(“”” & sMsg & “””)</script>”

sb = New System.Text.StringBuilder

For Each oFormObject In frm.Controls
If TypeOf oFormObject Is HtmlForm Then
Exit For
End If
oFormObject.Controls.AddAt(oFormObject.Controls.Count, New LiteralControl(sb.ToString()))
End Sub


On the button click event i write a JavaScript method to the page, the JavaScript method is displaying the message your pass to the method  showMsg().

Please don’t pass special characters as the message, as they will conflict with the special characters of JavaScript and message won’t come properly.

For more articles updates and sourcecodes please visit dotnetcoderoom