当前位置:编程学习 > C#/ASP.NET >>

asp.net try catch问题

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部分的代码执行了,但是为什么在页面上没有弹出添加失败,请尝试重新添加的提示框? --------------------编程问答-------------------- 你把 try 中的“return;”取消 --------------------编程问答-------------------- 设个断点调一下~~ --------------------编程问答--------------------

Page.ClientScript.RegisterStartupScript(this.GetType(), "myscript", "<script>alert('添加失败,请尝试重新添加')</script>");

应该这个写的有些问题

试试这个: Page.ClientScript.RegisterStartupScript(this.GetType(), "myscript", alert('添加失败,请尝试重新添加');",true);

你alert 脚本后面没有加";"号 --------------------编程问答-------------------- 请确保确实捕获到了异常,如下是没有问题的.可以正常弹出
try
            {
                int j = 0;
                int i = 1 / j;
            }
            catch (Exception ex)
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "myscript", "<script>alert('添加失败,请尝试重新添加')</script>");
            }
--------------------编程问答-------------------- catch 中的代码执行了,说明try里面的代码有异常, 看下 ex 的 消息是什么错就知道了 --------------------编程问答--------------------  改成这个试试Response.Write("<script>window.alert('提交失败');</script>"); --------------------编程问答-------------------- 我试了把catch中的提示框改成Page.ClientScript.RegisterStartupScript(this.GetType(), "myscript", "<script>alert('"+ex.Message+"')</script>");
这样也弹不出来,我加了断点看了,ex是有错误提示的值的,也是string类型的为什么就弹不出提示框呢? --------------------编程问答--------------------
response.write(“<script>alert('异常')</script>”)

这样写肯定没问题 --------------------编程问答-------------------- 写成这样:Page.ClientScript.RegisterStartupScript(this.GetType(), "myscript", "alert('"+ex.Message+"');",true);也弹不出 --------------------编程问答--------------------
引用 8 楼  的回复:
C# code

response.write(“<script>alert('异常')</script>”)

这样写肯定没问题

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



ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "alertScript", "alert('添加失败,请尝试重新添加');", true); --------------------编程问答--------------------  catch (Exception ex)
  {
  Page.ClientScript.RegisterStartupScript(this.GetType(), "myscript", "<script>alert('添加失败,请尝试重新添加')</script>");
  }  这个后面 加个 return --------------------编程问答-------------------- 打个断点,看哪句出的问题,如果你在update中把失败的exception处理了当然不会走Catch
如果catch走了 你看看正常情况下的 Alter成功了没   
如果没走的话 你可能把你的控件添加到了 updatePanel中 
慢慢 自己调吧 毕竟大家不能运行你的代码
--------------------编程问答-------------------- 弹出这个对话框之前还有没有弹出其他的对话框?如果有的话
Page.ClientScript.RegisterStartupScript(this.GetType(), "myscript", "<script>alert('添加失败,请尝试重新添加')</script>");
改成别的字符串或者GUID.NewGuid().ToString()试试。 --------------------编程问答-------------------- 楼上说的不少了 --------------------编程问答--------------------  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>");
  }
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,