The name 'MessageBox' does not exist in the current context

13.7k Views Asked by At
if (Session["Customer_ID"] == null)
{
     if (MessageBox.Show("Do you want to login or countinue without login?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
      {
          // user clicked yes redirects to login page
      }
      else
      {
          // user clicked no do something
      }
}         

it shows the "The name 'MessageBox' does not exist in the current context" error i have used System.Windows.Forms

2

There are 2 best solutions below

2
On

The best and simple way to deal with Yes/No confirmation in Javascript world is use confirm box. Refer this tutorial: http://www.w3schools.com/js/js_popup.asp

Here goes the code for it:

if (Session["Customer_ID"] == null)
{
      if (confirm("Do you want to login or countinue without login?") == true)
      {
          // user clicked yes redirects to login page
      }
      else
      {
          // user clicked no do something
      }
} 

The above approach will Prompt user with OK & CANCEL buton only but if you really want to change the button to YES/NO then you have to write your own Alert box. Refer this tutorial for further details: http://www.codeproject.com/Articles/25640/ASP-NET-Csharp-MessageBox

0
On

As there is no MessageBox in ASP.NET. You can simply do "Message box" by this line:

Response.Write("<script>alert('Your message in the box');</script>");

Working in MSVS 2015 (14.09.16)