I want to use a message box in c# website.I have tried MessageBox.show() but it is not working.Please tell me how can I do this.
Asked
Active
Viewed 1,369 times
0
-
Right click on MessageBox and see if you a get an option Resolve (first). If yes follow it and add the Windows.Form namespace – idipous Oct 19 '13 at 10:56
-
Use of a WinForms or WPF method in an ASP.NET application will not work. Even thinking it might indicates you are just beginning. I suspect your best bet is to find an introduction to ASP.NET and web applications. – Richard Oct 19 '13 at 10:57
-
Similar question: [ASP.NET Web Application Message Box][1] [1]: http://stackoverflow.com/questions/9720143/asp-net-web-application-message-box – Krekkon Oct 19 '13 at 10:57
-
You don't *really* want to have somebody break into the server room to click the OK button. So MessageBox is suppressed in ASP.NET. Keep in mind that your web page runs in the user's browser, a Javascript alert() is the functional equivalent. – Hans Passant Oct 19 '13 at 12:39
4 Answers
1
you cannot use a MessageBox on an ASP.NET website. There is no such thing. MessageBox is only for WPF/Winforms, not webforms. The only way to show a messagebox would be to use JavaScript's alert() function.
what are you trying to achieve here?

Ahmed ilyas
- 5,722
- 8
- 44
- 72
-
-
-
-
I have also tried this code:- protected void Button1_Click(object sender, EventArgs e) { System.Windows.Forms.MessageBox.Show("Hello"); } But it throws following exception:- The type or namespace name 'Windows' does not exist in the namespace 'System' (are you missing an assembly reference?)Please tell me its solution. – user2893698 Oct 19 '13 at 11:56
-
no, this is NOT the solution as I have said earlier :) Please take a look at the responses below where ClientScriptManager is being used. – Ahmed ilyas Oct 19 '13 at 15:22
1
You can use extension method to that.
public void ShowMessageBox(string messageString)
{
ClientScript.RegisterStartupScript(
this.GetType(), "myalert", "alert('" + messageString + "');", true);
}

Thilina H
- 5,754
- 6
- 26
- 56
0
Yes, MessageBox.show() won't work in web pages because c# class file codes are executing in server side. but you need to display the message in client side.
Add this function in your class file and call by passing your message.
void DisplayMsg(string cMsg)
{
Type cstype = this.GetType();
ClientScriptManager cs = Page.ClientScript;
String cstext = "alert('" + cMsg + "');";
cs.RegisterStartupScript(cstype, "PopupScript", cstext, true);
}

Uwe Keim
- 39,551
- 56
- 175
- 291

Raghuveera
- 320
- 3
- 9
- 27
-1
void ShowMessage()
{
Response.Write("<script>alert('Message here');</script>");
}
-
Downvoted because sometimes you will get an error during runtime that the tags are not in the write place when the page is being rendered in addition to AJAX issues too. – Ahmed ilyas Oct 19 '13 at 15:22