views:

291

answers:

3

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...

+8  A: 

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
how do i work with javscript messageboxes... is there an example similar to what i need... thanks
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
A: 

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