Search code examples
c#asp.netmessagebox

Issue Using Winforms MessageBox in ASP.NET


I have a MessageBox display when submitting a stream read file. However the box keeps coming up even after clicking okay. When I hold down the Enter key to fly through the boxes, only one field gets added from the file.

Heres my codebehind

if (FileTypeDDL.SelectedValue == "Calendar Dates" && fileName == "calendar_dates.txt")
{
    //Check if full txt has already been uploaded
    SEPTA_DS.CalendarDatesTBLDataTable GetCalendarDates = (SEPTA_DS.CalendarDatesTBLDataTable)cdta.GetDataByCategory(Convert.ToString(Session["Cat"]));
    var category = Convert.ToString(CategoryDDL.SelectedItem);
    var service_id = Convert.ToString(row["service_id"]);
    var date = Convert.ToString(row["date"]);
    var exception_type = Convert.ToString(row["exception_type"]);
    if (GetCalendarDates.Rows.Count < 1)
    {
        int insertData = Convert.ToInt32(cdta.InsertCalendarDates(category, service_id, date, exception_type));
    }
    else
    {
        DialogResult result = MessageBox.Show("This will overwrite the current list. Are you sure you wish to continue?", "Important Message",
        MessageBoxButtons.YesNo,
        MessageBoxIcon.Exclamation);
        if (result == DialogResult.Yes)
        {
            int updateData = Convert.ToInt32(cdta.UpdateCalendarDates(category, service_id, date, exception_type, Convert.ToInt32(GetCalendarDates.Rows[0]["CalendarID"])));
        }
        else
        {
            Response.Redirect("~/Import.aspx");
        }
    }
}

I just want the box to appear once and on 'Yes' insert the data. Why and how can I accomplish this.

EDIT: I just realized that this code is within a foreach loop and that's why it's reoccurring. My new question; How do I only show this MessageBox once?


Solution

  • As others have noted, you don't want to use the WinForms MessageBox method in your ASP.NET pages. It "works," but what is happening is that a messagebox appears on the server. You see the messagebox because you are developing locally, but if this code were pushed out to a production site visitors wouldn't see anything - the page would just hang, waiting for someone sitting at the web server to click the OK button! :-)

    To display a messagebox to a user you need to use client-side script. There are two JavaScript functions you should look into:

    • confirm - displays a dialog box with Ok and Cancel options - this is what you probably want.
    • alert - displays a dialog box with a single Ok button.

    I take it you have a button that the user is clicking to kick off this process. Look into using the OnClientClick property of the Button/LinkButton/ImageButton controls. First, try setting it to something like this:

    OnClientClick="return confirm('Are you sure you want to do this?');"
    

    This will display a messagebox with Ok/Cancel options when the user clicks the button. If they click Cancel the messagebox returns false and the postback is stopped. If they click Ok, it goes through.

    Now, your situation is a little more challenging because you only want to show this if some condition holds, namely if GetCalendarDates.Rows.Count >= 1. The tricky part is that you need to set this JavaScript via the OnClientClick property before the postback. So maybe in Page_Load you can programmatically set the button's OnClientClick property, but only if the condition of interest holds. Something like:

    void Page_Load(...)
    {
        if (GetCalendarDates.Rows.Count >= 1)
            myButton.OnClientClick = "return confirm('...');";
    }
    

    I'm not sure how or when GetCalendarDates is being set, but you may be best to have the above logic appear whenever GetCalendarDates is set. I presume this is a GridView? If so, consider adding it to the GridView's DataBound event handler.

    Happy Programming!