Hiii Alll

I need to display a message to the user after clicking the button control. How to do this in c#.net? I could not find Msgbox() option as we hav in vb.net.

can any one help me

8 Years
Discussion Span
Last Post by walflo

If I do so Am gettin this error
Error The name 'MessageBox' does not exist in the current context
Do we hav to create a separate function or to use any namespace for this.


I guess the Call is done within a Form?
The MessageBox is included in the namespace System.Windows.Forms
Do you have this in your using at the top?
Or have you tried to write

System.Windows.Forms.MessageBox.Show("This is a message");

MessageBox is only available in Window Applicaitons. In ASP.NET, you cannot use the MessageBox class or MsgBox function like you can in a Window Applicaitons, but you can emulate this functionality by streaming out a javascript alert instruction like this:

Response.Write(@"<script language='javascript'>alert('This is my Message')</script>");

In ASP.NET with code Behind VB I have used msgbox().
It worked well.
But in C# :( am gettin into trouble


This is my sample code for Palindrome checking.
Got a string in textbox 1.

TextBox2.Text = StrReverse(TextBox1.Text)
            If TextBox1.Text = TextBox2.Text Then
                MsgBox("String is palindrome", MsgBoxStyle.OkOnly)
                MsgBox("Not a palindrome", MsgBoxStyle.OkOnly)
            End If

This code is done in Asp.Net webapplication with VB code behind.
Can u say how to use message box with C# code behind:?:


So this is a Web application in c#.net
Well then you cannot use the Messagebox. This function / method
is used within an windows application
As mention earlier , you can solve this by using Javascript or ms.Ajax (Popup /modalpopup)


Hmmm Fine....
In C# we have to use script for messageBox.Okay let me do.


Select the function name "MsgBox", right click and from the menu select Go to Defenition and post the fuction defenition here. I suppose its a user defined function


Hi again

I yust wonder who this could be done..
Do as i told over.. in button click method

System.Windows.Forms.MessageBox.Show("This is a message");

then select: project -> add reference
Then scroll down to System.Windows.Forms and select this.
Press ok



I dont think what Frank is saying will work when deployed (But i will gladly be proven wrong as i would like to be able to use the messagebox functionality in an asp.net app). The messagebox will throw an exception because you can not show a modal form on a clients machine when it is created server side. The only real option avaliable is to Use javascript or Ajax as someone has mentioned earlier in this thread.


Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "SCRIPT", string.Format("alert('Hello World')"), true);


This is the only solution that works within this post:
Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "SCRIPT", string.Format("alert('Hello World')"), true);

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.