开发者

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 real开发者_如何学Goized 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?


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!


Options:

  • Consider Yes, Yes To all, No, No to all kind of Custom message boxes.
  • Collect all the required data and at the end of loop show a message box with all the data.
  • A custom message box with 'remember my action for the session'


Are you using the System.Windows.Forms.MessageBox?

If so then you can't use that in ASP.Net, it's for windows sessions only. (You will recieve a not logged in error)

Either that or the ASP.Net tag on your question is incorrect.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜