c# - 如何处理页面外的异常?
问题描述
我有一个 C# 项目,我正在尝试将其转换为 ASP.NET 和 C#。这是我的第一个 ASP.NET 项目。
我的用户非常喜欢我以前的项目,因为它灵活且易于使用。我正在尝试尽可能多地复制它。
我设计了一个名为 DM_Services.aspx 的页面,其中包含 3 个面板:
- 面板 1 有一个搜索按钮和几个用于过滤的字段
- 面板 2 有几个用于编辑的字段
- 面板 3 有一个网格视图。
最终目标是用户可以输入过滤器并单击搜索以在gridview中加载数据。
用户可以单击 gridview 中的一行,它将加载面板 2 中的字段,其中包含用于查看和编辑的数据。
我有一个 C# 文件SQLControl.cs
,我用它来管理数据访问。
我在该部分的该页面上不断收到错误HasException
消息。
如果我使用以下行:
ClientScriptManager.RegisterOnSubmitStatement(Me.GetType(), "ConfirmSubmit", exception);
我收到以下错误:
当前上下文中不存在名称“我”
如果我使用:
ScriptManager.RegisterStartupScript(Page, this.GetType(), "alert", "alert(exception);", true);
我得到错误:
'Page' 是当前上下文中不存在的类型
如果我使用以下行:
ScriptManager.RegisterStartupScript(This, this.GetType(), Guid.NewGuid().ToString("N"), "alert(exception);", true);
我收到此错误:
当前上下文中不存在名称“This”
如果我使用:
ScriptManager.RegisterStartupScript(this, this.GetType(), Guid.NewGuid().ToString("N"), "alert(exception);", true);
然后我得到这个错误:
参数 1:无法从“Productivity_ASPWeb.SQLControl”转换为“System.Web.UI.Control”
我很确定我收到了这个错误,因为它在SQLControl.cs
页面之外。
那么我该如何处理异常呢?
public bool HasException(bool Report = false)
{
if (string.IsNullOrEmpty(exception))
return false;
if (Report == true)
ClientScriptManager.RegisterOnSubmitStatement(Me.GetType(), "ConfirmSubmit", exception);
ScriptManager.RegisterStartupScript(Page, this.GetType(), "alert", "alert(exception);", true);
ScriptManager.RegisterStartupScript(this, this.GetType(), Guid.NewGuid().ToString("N"), "alert(exception);", true);
ScriptManager.RegisterStartupScript(This, this.GetType(), Guid.NewGuid().ToString("N"), "alert(exception);", true);
return true;
}
解决方案
我从 SQLControl.cs 中删除了 scriptManager 并将代码更改为:
public bool HasException(bool Report = false)
{
if (string.IsNullOrEmpty(exception))
return false;
else
return true;
}
然后我将脚本管理器放在 aspx 页面中:
protected void Page_Load(object sender, EventArgs e)
{
ScriptManager.RegisterStartupScript(Page, this.GetType(), "alert", "alert(exception);", true);
}
推荐阅读
- java - 什么都不做的抽象方法实现
- android - AAPT:错误:找不到资源 android:attr/colorError
- android - 是否可以使用 xamarin 识别应用程序内的 wifi 网络
- chatbot - 当我使用 LogicAdpater 时如何运行聊天机器人
- python - Python 中的 Matplotlib 并行绘图
- python - 将行插入空 Pandas DF
- python - Python/kivy - 文本输入到标签
- sql - SQL IsNumeric 函数
- perl - 我可以抑制“%s 的 Perl API 版本 %s 与 %s 不匹配”错误吗
- multithreading - 在单独的线程中关闭 CameraDevice