C# 中用于 ASP.NET FormView 的异常处理

本文关键字:FormView 异常处理 NET ASP 用于 | 更新日期: 2023-09-27 18:37:08

我正在尝试在表单视图的项目插入中进行一些错误处理。如果有人忘记在教程部分输入值,错误消息将检测到它是空的,并弹出它为空的弹出窗口。但是,我的Visual Studio无法识别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;
    }
}

C# 中用于 ASP.NET FormView 的异常处理

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";
     }
}