0

I have an exception where i need to sheo a messagebox

my messagebox works on localhost but not on the server

catch (Exception)
        {

            MessageBox.Show("Machine Cannot Be Deleted", "Delete from other Places first", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

how can i make this work... thanks

is there another way to do this.... please help.. i know this is a small problem but it needs to be done...

user175084
  • 4,550
  • 28
  • 114
  • 169
  • Did you mean for this question to be tagged asp.net? You cannot use MessageBox.Show in asp.net your best bet is to use RegisterClientSideScript to register some javascript which pops up an alert....I wouldn't encourage this for exception handling though. – Simon Fox Nov 26 '09 at 21:58
  • You have to use the namespace System.Windows.Forms – priyanka.sarkar Nov 29 '09 at 07:12

2 Answers2

8

You can't use a Windows Form MessageBox in ASP.NET since it runs on the server side, making it useless for the client.

Look into using a Javascript alert or some other type of validation error. (Maybe have a hidden control with your error message and toggle its Visibility in the catch block or use Response.Write for a Javascript alert).

Something like this (untested):

Response.Write("<script language='javascript'>window.alert('Machine Cannot Be Deleted, delete from other places first.');</script>");
Brandon
  • 68,708
  • 30
  • 194
  • 223
  • how do i work with javscript messageboxes... is there an example similar to what i need... thanks – user175084 Nov 26 '09 at 22:00
  • Basic alerts: http://www.c-point.com/javascript_tutorial/javascript_message_boxes.htm You just need to find a way to show them in a page. You can write directly to the response like I've shown, or use the RegisterClientScriptBlock method on the ScriptManager class. In case the user has javascript disabled, I would recommend toggling an error message on the page itself as a fall back. – Brandon Nov 26 '09 at 22:05
0

You have to use the namespace System.Windows.Forms and then you can use Message box property

e.g.

   using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;

**using System.Windows.Forms;**

    public partial class _Default : System.Web.UI.Page 
   {
      protected void Page_Load(object sender, EventArgs e)
       {
          MessageBox.Show("Machine Cannot Be Deleted", "Delete from other Places                   
          first", MessageBoxButtons.OK, MessageBoxIcon.Error);

       }    
    }

Among the other alternatives (apart from the one Mr.Brandon has proposed)

a) Use javascript

e.g.

Response.Write("<script>alert('Machine Cannot Be Deleted')</script>");

b) Make a custom function that will work like a message box

e.g.

protected void Page_Load(object sender, EventArgs e)
    {
        MyCustomMessageBox("Machine Cannot Be Deleted");
    }

    private void MyCustomMessageBox(string msg)
    {
        Label lbl = new Label();
        lbl.Text = "<script language='javascript'>" + Environment.NewLine + "window.alert('" + msg + "')</script>";
        Page.Controls.Add(lbl);
    }

Hope this helps

priyanka.sarkar
  • 25,766
  • 43
  • 127
  • 173