【问题标题】:Exception Handling in C# for ASP.NET FormViewASP.NET FormView 的 C# 中的异常处理
【发布时间】:2015-01-12 10:47:31
【问题描述】:

我正在尝试在为我的表单视图插入项目时进行一些错误处理。如果有人忘记在教程部分输入一个值,错误消息将检测到它为 null 并弹出它为 null。但是,我的视觉工作室无法识别 Exception 或 ExceptionHandled。错误是没有异常的定义。我搜索了高低,我不知道问题是什么。任何想法将不胜感激。

protected void FormView1_ItemInserting(object sender, FormViewInsertEventArgs e)
{

    if (e.Exception != Null)
    {
        string msg = "";
        if (e.Exception.Message.Containts("NULL"))
        {
            msg = "Tutorial Section cannot be empty.";
        }
        else
            msg = "unknown";
        ClientScript.RegisterStartupScript(this.GetType(), "myalert", "window.setTimeout(\"alert('" + msg + "')\",0);", true);
        e.ExceptionHandled = true;
    }
}

【问题讨论】:

  • 你所说的我猜是指验证而不是错误处理。
  • 不是这样。我只是想知道为什么无法识别 e.Exception。
  • @NegarNegaru 它根本不存在。 FormViewInsertEventArgs Class
  • 我明白了。我假设因为它存在于 listview 中,它也应该存在于 formview 中。谢谢。

标签: c# asp.net exception-handling


【解决方案1】:

protected void FormView1_ItemInserting 事件没有Exception

您必须使用Inserted 事件处理程序,而不是Inserting 事件!

像这样:

protected void FormView1_ItemInserted(object sender, FormViewInsertedEventArgs e)
{
    if (e.Exception != null)
    {
        Label1.Text = "Error : " + e.Exception.Message;

        e.ExceptionHandled = true;
    }
     else
     {
        Label1.Text = "Inserted Successfully";
     }
}

【讨论】:

    猜你喜欢
    • 2014-12-31
    • 2019-08-31
    • 1970-01-01
    • 1970-01-01
    • 2010-10-08
    • 2011-09-30
    • 2014-12-19
    • 2010-10-29
    • 1970-01-01
    相关资源
    最近更新 更多