繁体   English   中英

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

[英]Exception Handling in C# for ASP.NET FormView

我试图在为Formview插入项目时进行一些错误处理。 如果有人忘记在“教程”部分中输入值,则错误消息将检测到它为空,并弹出一个为空的值。 但是,我的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;
    }
}

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM