首页 > 解决方案 > 如何处理页面外的异常?

问题描述

我有一个 C# 项目,我正在尝试将其转换为 ASP.NET 和 C#。这是我的第一个 ASP.NET 项目。

我的用户非常喜欢我以前的项目,因为它灵活且易于使用。我正在尝试尽可能多地复制它。

我设计了一个名为 DM_Services.aspx 的页面,其中包含 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;
    }

标签: c#asp.netexceptiontypes

解决方案


我从 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);
    }

推荐阅读