简体   繁体   中英

message box not working

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

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>");

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

eg

   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

eg

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

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

eg

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM