Reputation: 4640
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...
Upvotes: 0
Views: 5058
Reputation: 26538
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
Upvotes: 0
Reputation: 70022
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>");
Upvotes: 8