0

I have a pop out message box, what I want is the message box always pop out in front of the browser when it is clicked, but the problem is it sometimes pop out behind the browser. Anything I can do to make sure the message box is always pop out in front the browser? Thanks.

protected void Button1_Click(object sender, EventArgs e)
{
    string appointmentdate = Convert.ToString(DropDownListDay.Text + "-" + DropDownListMonth.Text + "-" + DropDownListYear.Text);
    string appointmenttime = Convert.ToString(DropDownListTime.Text);


    using (SqlConnection con = new SqlConnection("Data Source=USER-PC;Initial Catalog=webservice_database;Integrated Security=True"))
    {
        con.Open();
        SqlCommand data = new SqlCommand("Select COUNT(*) from customer_registration where adate='" + appointmentdate + "'AND atime='" + appointmenttime + "'", con);
        Int32 count = (Int32)data.ExecuteScalar();
        if (count == 0)
        {
            SqlConnection con1 = new SqlConnection("Data Source=USER-PC;Initial Catalog=webservice_database;Integrated Security=True");
            SqlCommand cmd = new SqlCommand("UPDATE customer_registration SET servicetype = @servicetype, comment = @comment, adate = @adate, atime = @atime where username='" + Session["username"] + "'", con1);
            con1.Open();

            cmd.Parameters.AddWithValue("@servicetype", DropDownListServicetype.Text);
            cmd.Parameters.AddWithValue("@comment", TextBoxComment.Text);
            cmd.Parameters.AddWithValue("@adate", DropDownListDay.Text + "-" + DropDownListMonth.Text + "-" + DropDownListYear.Text);
            cmd.Parameters.AddWithValue("@atime", DropDownListTime.Text);
            cmd.ExecuteNonQuery();

            con1.Close();
            Response.Redirect("MakeAppointmentSuccess.aspx");
        }
        else
        {
            MessageBox.Show("This appointment is not available. Please choose other date & time.");
            con.Close();
        }
    }
}
Ching
  • 93
  • 2
  • 4
  • 13

3 Answers3

2

You're mixing the Win32 message box with ASP.NET's Response.Redirect. Since this post is tagged ASP.NET and you're calling Response.Redirect, I'm assuming this is an ASP.NET application, and not a WinForms or WPF application.

What is happening is that the message box is popping up on the "server", while the browser is the "client", which on your developer machine is the same thing. Never call MessageBox.Show from an ASP.NET application. The reason why is that the message box you are seeing is not coming from the browser, and once you deploy this to a true server, clients will never see the message box and your server may or may not be flooded with message box windows (depending on which user is running and what the privileges are).

In order to make a "MessageBox"-style alert in the browser, you must use the JavaScript alert() function. You can either do this on the HTML (ASPX) or JS files rendered by the browser, or by calling ScriptManager.RegisterStartupScript. Take a look at the answers to this SO question for details: ScriptManager.RegisterStartupScript code not working - why?

Community
  • 1
  • 1
Paul
  • 3,748
  • 1
  • 25
  • 28
0

If you're using Windows Forms invoke this prototype :

public static DialogResult Show(
    IWin32Window owner,
    string text
)

and pass Process.GetCurrentProcess().MainWindowHandle as IWin32Window owner.

Instead, if you're using WPF invoke this prototype :

public static MessageBoxResult Show(
    Window owner,
    string messageBoxText
)

and pass Application.MainWindow as owner.

Antonio Petricca
  • 8,891
  • 5
  • 36
  • 74
-1

Can you use the overload for MessageBox.Show() that accepts an IWin32Window as the first parameter? You should be able to pass "this" as the first parameter:

MessageBox.Show(this, "This appointment is not available. Please choose other date & time.");

I suspect that won't help though, because if you omit that parameter, it should choose as a parent window the currently active one anyway...

If you're absolutely desperate you could do this:

MessageBox.Show
(
    "This appointment is not available. Please choose other date & time.",
    Application.ProductName,
    MessageBoxButtons.OK,
    MessageBoxIcon.Information,
    MessageBoxDefaultButton.Button1,
    MessageBoxOptions.DefaultDesktopOnly
);

I don't recommend it though - it's a system modal dialog that will be in front of every other application.

Matthew Watson
  • 104,400
  • 10
  • 158
  • 276