[英]Issue Using Winforms MessageBox in ASP.NET
I have a MessageBox
display when submitting a stream read file. 提交流读取文件时,我有一个MessageBox
显示。 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. 当我按住Enter键在各框之间移动时,文件中仅添加了一个字段。
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. 编辑 :我刚刚意识到这段代码是在一个foreach
循环内,这就是为什么它会重复发生。 My new question; 我的新问题; How do I only show this MessageBox
once? 如何只显示一次此MessageBox
?
As others have noted, you don't want to use the WinForms MessageBox method in your ASP.NET pages. 正如其他人指出的那样,您不想在ASP.NET页中使用WinForms MessageBox方法。 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! 您看到消息框是因为您是在本地开发的,但是如果将此代码推送到生产站点,则访问者将看不到任何内容-该页面只会挂起,等待坐在Web服务器上的人单击OK按钮! :-) :-)
To display a messagebox to a user you need to use client-side script. 要向用户显示消息框,您需要使用客户端脚本。 There are two JavaScript functions you should look into: 您应该研究两个JavaScript函数:
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. 查看使用Button / LinkButton / ImageButton控件的OnClientClick属性。 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. 如果单击取消,则消息框将返回false,并且回发将停止。 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 . 现在,您的情况更具挑战性,因为您只想在满足某些条件(即GetCalendarDates.Rows.Count> = 1)的情况下显示此情况。 The tricky part is that you need to set this JavaScript via the OnClientClick property before the postback. 棘手的部分是您需要在回发之前通过OnClientClick属性设置此JavaScript。 So maybe in Page_Load you can programmatically set the button's OnClientClick property, but only if the condition of interest holds. 因此,也许在Page_Load中,您可以以编程方式设置按钮的OnClientClick属性,但前提是需要满足条件。 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. 我不确定如何设置或何时设置GetCalendarDates,但是最好在设置GetCalendarDates时让上述逻辑出现。 I presume this is a GridView? 我认为这是一个GridView吗? If so, consider adding it to the GridView's DataBound event handler. 如果是这样,请考虑将其添加到GridView的DataBound事件处理程序中。
Happy Programming! 快乐编程!
Options: 选项:
Are you using the System.Windows.Forms.MessageBox? 您正在使用System.Windows.Forms.MessageBox吗?
If so then you can't use that in ASP.Net, it's for windows sessions only. 如果是这样,那么您将无法在ASP.Net中使用它,它仅适用于Windows会话。 (You will recieve a not logged in error) (您将收到一个未登录的错误)
Either that or the ASP.Net tag on your question is incorrect. 您的问题的ASP.Net标记或该标记不正确。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.