Mega Code Archive

 
Categories / ASP.Net Tutorial / Development
 

Transfer to different page based on exception type

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"      Inherits="Default"%> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server">     <title>Exceptions at work</title> </head> <body>     To test this page effectively, first disable the customErrors section in the web.config file and                  then try again with the section enabled.     <div id="pageContent">         <form id="form1" runat="server">             <ul><li>             <asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click">Click to throw a NotImplementedException exception</asp:LinkButton>              </li><li>             <asp:LinkButton ID="LinkButton2" runat="server" OnClick="LinkButton2_Click">Click to generate an internal error</asp:LinkButton></li>                 <li>             <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="missing.aspx">Click to raise a HTTP 404 error</asp:HyperLink></li>             </ul>         </form>     </div> </body> </html> File: Default.aspx.cs using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; public partial class Default : System.Web.UI.Page {     protected void Page_Error(object sender, EventArgs e)     {         Exception ex = Server.GetLastError();         if (ex is NotImplementedException)             Server.Transfer("/notimplementedexception.aspx");         else             Server.Transfer("/apperror.aspx");         Server.ClearError();     }     protected void Page_Load(object sender, EventArgs e)     {     }   protected void LinkButton1_Click(object sender, EventArgs e)   {     throw new NotImplementedException("The feature you requested is not implemented yet.");   }   protected void LinkButton2_Click(object sender, EventArgs e)   {         string test = null;         Response.Write(test.ToString());   } }