在ASP.NET webform项目中显示错误/异常消息。 [英] Show error/exception message in ASP.NET webform project.

查看:67
本文介绍了在ASP.NET webform项目中显示错误/异常消息。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的asp.net web-form项目中有.cs文件(代码文件),其中我做了数据库相关的连接和CRUD操作。现在我想处理一个异常,如果发生,我用try catch块。当异常发生时,它将被抛出,但我想向用户显示发生异常的消息。我知道有一个javascript的选项,但它只是一个代码文件,没有页面,没有脚本管理器。请帮忙怎么做



我尝试过:



在我的asp.net web-form项目中有.cs文件(代码文件),其中我做了数据库相关的连接和CRUD操作。现在我想处理一个异常,如果发生,我用try catch块。当异常发生时,它将被抛出,但我想向用户显示发生异常的消息。我知道有一个javascript的选项,但它只是一个代码文件,没有页面,没有脚本管理器。请帮助如何执行此操作

in my asp.net web-form project there is .cs file (code file) where i did database related connections and CRUD operation. Now I want to handle a exception if occurred, I used try catch block. When exception occurred it will be thrown but I want to show message to a user that perticulare exception occured. I know there is a option of javascript but it is just a code file no page is there and no scriot manager is tthere. Please help how to do this

What I have tried:

in my asp.net web-form project there is .cs file (code file) where i did database related connections and CRUD operation. Now I want to handle a exception if occurred, I used try catch block. When exception occurred it will be thrown but I want to show message to a user that perticulare exception occured. I know there is a option of javascript but it is just a code file no page is there and no scriot manager is tthere. Please help how to do this

推荐答案

您可以轻松地将.cs中的javascript代码注入aapx页面。那个。我认为这就是你需要的。
You can easily inject javascript codes from .cs to the aapx page. Ty that. I think that is what you need.


你所要做的就是把错误抛给子类。



假设您有 class 称为 Business ,因为



All you have to do is, throw the error in the child class.

Suppose you have class called Business as

public class Business
  {
      public string SomeMethod()
      {
          try
          {
              int x = 0;
              int a = 5 / x;
          }
          catch (Exception)
          {

              throw;  // YOu have to throw the error to parent
          }
          return "";
      }
  }



,你是从一个页面调用它..




and you are calling it from a page..

protected void Page_Load(object sender, EventArgs e)
      {

          try
          {
              Business obj = new Business();
              obj.SomeMethod();
          }
          catch (Exception ex)
          {
              ShowMessage(ex.Message);
          }



      }



帮助功能显示来自cs文件的消息




Helper funciton to show message from cs file

public void ShowMessage(string message)
     {

         System.Text.StringBuilder sb = new System.Text.StringBuilder();
         sb.Append("<script type = 'text/javascript'>");
         sb.Append("window.onload=function(){");
         sb.Append("alert('");
         sb.Append(message);
         sb.Append("')};");
         sb.Append("</script>");
         ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", sb.ToString());
     }


这篇关于在ASP.NET webform项目中显示错误/异常消息。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆