try
        {
                        int result = ud.inTea(u);
            if (result > 0)
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "myscript", "<script>alert('添加成功')</script>");
                return;
            }
            else
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "myscript", "<script>alert('添加失败,请尝试重新添加')</script>");
                return;
            }
        }
        catch (Exception ex)
        {
            Page.ClientScript.RegisterStartupScript(this.GetType(), "myscript", "<script>alert('添加失败,请尝试重新添加')</script>");
            }
这个是我写的try catch在try中有异常噶时候 catch部分的代码执行了,但是为什么在页面上没有弹出添加失败,请尝试重新添加的提示框?

解决方案 »

  1.   

    你把 try 中的“return;”取消
      

  2.   

    Page.ClientScript.RegisterStartupScript(this.GetType(), "myscript", "<script>alert('添加失败,请尝试重新添加')</script>");应该这个写的有些问题试试这个: Page.ClientScript.RegisterStartupScript(this.GetType(), "myscript", alert('添加失败,请尝试重新添加');",true);你alert 脚本后面没有加";"号
      

  3.   

    请确保确实捕获到了异常,如下是没有问题的.可以正常弹出
    try
                {
                    int j = 0;
                    int i = 1 / j;
                }
                catch (Exception ex)
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "myscript", "<script>alert('添加失败,请尝试重新添加')</script>");
                }
      

  4.   

    catch 中的代码执行了,说明try里面的代码有异常, 看下 ex 的 消息是什么错就知道了
      

  5.   

     改成这个试试Response.Write("<script>window.alert('提交失败');</script>");
      

  6.   

    我试了把catch中的提示框改成Page.ClientScript.RegisterStartupScript(this.GetType(), "myscript", "<script>alert('"+ex.Message+"')</script>");
    这样也弹不出来,我加了断点看了,ex是有错误提示的值的,也是string类型的为什么就弹不出提示框呢?
      

  7.   

    response.write(“<script>alert('异常')</script>”)
    这样写肯定没问题
      

  8.   

    写成这样:Page.ClientScript.RegisterStartupScript(this.GetType(), "myscript", "alert('"+ex.Message+"');",true);也弹不出
      

  9.   

    这样弹出的话页面会刷新,如果用Page.ClientScript.RegisterStartupScript页面就不会刷新
      

  10.   

    ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "alertScript", "alert('添加失败,请尝试重新添加');", true);
      

  11.   

     catch (Exception ex)
      {
      Page.ClientScript.RegisterStartupScript(this.GetType(), "myscript", "<script>alert('添加失败,请尝试重新添加')</script>");
      }  这个后面 加个 return
      

  12.   

    打个断点,看哪句出的问题,如果你在update中把失败的exception处理了当然不会走Catch
    如果catch走了 你看看正常情况下的 Alter成功了没   
    如果没走的话 你可能把你的控件添加到了 updatePanel中 
    慢慢 自己调吧 毕竟大家不能运行你的代码
      

  13.   

    弹出这个对话框之前还有没有弹出其他的对话框?如果有的话
    Page.ClientScript.RegisterStartupScript(this.GetType(), "myscript", "<script>alert('添加失败,请尝试重新添加')</script>");
    改成别的字符串或者GUID.NewGuid().ToString()试试。
      

  14.   

     catch (Exception ex)
      {
      Page.ClientScript.RegisterStartupScript(this.GetType(), "myscript", "<script>alert('添加失败,请尝试重新添加')</script>");
      }
    =>
     catch (Exception ex)
      {
      Page.ClientScript.RegisterStartupScript(this.GetType(), "myscript"+ex.Message, "<script>alert('添加失败,请尝试重新添加')</script>");
      }