[英]Asp.net MessageBox doesn't exist in context
I have created a connection class in asp.net to save values in list i have written following code but it is generating error that MessageBox
doesn't exist. 我在asp.net中创建了一个连接类,以将值保存在列表中。我编写了以下代码,但它生成了
MessageBox
不存在的错误。 I google this and have found few answers but these are also not working here my code is: 我用谷歌搜索,发现了很少的答案,但是这些在这里也不起作用,我的代码是:
public static class Connection
{
public static ArrayList GetCoffeeByType(string coffeetype)
{
ArrayList list = new ArrayList();
try
{
mydbEntities db = new mydbEntities();
var query = from p in db.tableabc select p;
list = query.ToList();
}
catch (Exception ex)
{
MessageBox.Show(exceptionObj.Message.ToString());
}
return list;
}
}
I tried this System.Web.UI.ScriptManager.RegisterStartupScript(this, typeof(Page), "alert", "alert('" + Message + "')", true);
我尝试了这个
System.Web.UI.ScriptManager.RegisterStartupScript(this, typeof(Page), "alert", "alert('" + Message + "')", true);
-- But it is also showing error at this
and Message
-- how can i show this MessageBox? -但是它也在
this
和Message
显示错误-如何显示此MessageBox?
You won't be able to invoke MessageBox.Show
in an ASP.NET application. 您将无法在ASP.NET应用程序中调用
MessageBox.Show
。 Try this instead: 尝试以下方法:
catch (Exception ex)
{
var script = "alert(" + System.Web.HttpUtility.JavaScriptStringEncode(ex.Message, true) + ")";
System.Web.UI.ScriptManager.RegisterStartupScript(this, typeof(Page), "alert", script, true);
}
Note that you need to get the Message
property on the caught exception, ex
, and you should use JavaScriptStringEncode
to safely escape the alert message. 请注意,您需要获取捕获到的异常
ex
的Message
属性,并且应该使用JavaScriptStringEncode
安全地转义警报消息。
This is a static class called Alert with one public method called Show. 这是一个名为Alert的静态类,具有一个称为Show的公共方法。 The implementation is as simple as can be.
实现非常简单。 Just put the .cs file in the App_Code folder on your website and you instantly have access to the method from all pages and user controls.
只需将.cs文件放入您网站上的App_Code文件夹中,即可立即从所有页面和用户控件访问该方法。
using System.Web;
using System.Text;
using System.Web.UI;
/// <summary>
/// A JavaScript alert
/// </summary>
publicstaticclass Alert
{
/// <summary>
/// Shows a client-side JavaScript alert in the browser.
/// </summary>
/// <param name="message">The message to appear in the alert.</param>
publicstaticvoid Show(string message)
{
// Cleans the message to allow single quotation marks
string cleanMessage = message.Replace("'", "\\'");
string script ="<script type=\"text/javascript\">alert('"+ cleanMessage +"');</script>";
// Gets the executing web page
Page page = HttpContext.Current.CurrentHandler as Page;
// Checks if the handler is a Page and that the script isn't allready on the Page
if (page !=null && !page.ClientScript.IsClientScriptBlockRegistered("alert"))
{
page.ClientScript.RegisterClientScriptBlock(typeof(Alert), "alert", script);
}
}
}
Demonstration 示范
That class enables you to add a JavaScript alert to any page at any time. 该类使您可以随时将JavaScript警报添加到任何页面。 Here is an example of a
Button.Click
event handler that uses the method for displaying status messages. 这是一个使用该方法显示状态消息的
Button.Click
事件处理程序的示例。
void btnSave_Click(object sender, EventArgs e)
{
try
{
Alert.Show("Your Message");
}
catch (Exeception ex )
{
Alert.Show(ex.Message);
}
}
In your Case : 就您而言:
public static class Connection
{
public static ArrayList GetCoffeeByType(string coffeetype)
{
ArrayList list = new ArrayList();
try
{
mydbEntities db = new mydbEntities();
var query = from p in db.tableabc select p;
list = query.ToList();
}
catch (Exception ex)
{
Alert.Show(ex.Message);
}
return list;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.