Questo esempio mostra come estendere la classe Page in C#

C#

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Diagnostics;

namespace Sgart.Root.Base
{
  /// <summary>
  /// Summary description for Base.
  /// </summary>
  public class PageBase : System.Web.UI.Page
  {
    private string eventLogFile = "";

    public PageBase(){}

    protected override void OnInit(EventArgs e)
    {
      base.OnInit(e);

      eventLogFile = string.Format("{0}\\{1}.log",
        Application["mapPath"].ToString() + 
        System.Configuration.ConfigurationSettings.AppSettings["eventLogPath"],
        DateTime.Today.ToString("yyyyMMdd"));
  
      this.PreRender += new System.EventHandler(this.PageBase_PreRender);
      this.Load += new System.EventHandler(this.PageBase_Load);
      //this.Error += new System.EventHandler(this.PageBase_Error);

      // imposta l'header della pagina
      /* imposta l'header della pagina
      System.Web.UI.Control c = this.FindControl("FormMain");
      c.Controls.AddAt(0,new Header(DB.GetConfigKey("app.title")));
      c.Controls.AddAt(1, mm);
      */

    }

    private void PageBase_Load(object sender, System.EventArgs e)
    {
      ...
    }

    private void PageBase_PreRender(object sender,  System.EventArgs e)
    {
      // imposta il footer della pagina
      /*
      System.Web.UI.Control c = this.FindControl("FormMain");
      c.Controls.Add(new Footer());
      */
    }

    protected void PageBase_Error(object sender, System.EventArgs e)
    {
      Exception error = Server.GetLastError();
      string errMsg = "<html><head><title>Error</title>" +
        "<link media=\"all\" href=\"models/common.css\" type=\"text/css\" rel=\"stylesheet\" />" +
        "<link href=\"favicon.ico\" type=\"image/x-icon\" rel=\"shortcut icon\" />" +
        "<body style=\"padding: 5px\">" +
        "<h1><a href=\"http://www.sgart.it/\" target=\"_blank\">" +
        "<img src=\"th.aspx?res=sgart.gif\" width=\"16\" height=\"16\" alt=\"logo Sgart.it\" align=\"middle\" />" +
        "</a> Errore nella pagina</h1><hr /><p><i>" +
        DateTime.Now.ToString("dd/MM/yyyy hh.mm.ss") + "</i>: Si è verificato un errore inaspettato</p>" +
        "<p>L'errore è stato scritto nel file: <b>" + eventLogFile + "</b></p>" +
        "<p>La pagina che ha generato l'errore e: <span style=\"color: #0000FF\">" + 
        Request.Url.ToString() + "</span>" + 
        "<br />Errore: <span style=\"color: #FF0000\">" + error.Message .ToString() + "</span></p>" + 
        "<hr /><pre>" + error.ToString() + "</pre>" +
        "<hr /><span style=\"color: #c0c0c0\">by sgart.it</span>" + 
        "</body></html>";
      Response.Write(errMsg);
      LogEvent(error.ToString());
      
      Server.ClearError();  
    }

    private void LogEvent(string message)
    {

      try 
      {
        System.IO.StreamWriter stream = new System.IO.StreamWriter(eventLogFile, true);
        stream.WriteLine(DateTime.Now.ToString("dd/MM/yyyy hh.mm.ss"));
        stream.WriteLine(message);
        stream.WriteLine("");
        stream.Close();
      } 
      catch (Exception ex) 
      {
        Response.Write("<hr /><h3 style=\"color: #FF0000\">NON RIESCO A SCRIVERE NEL LOG</h3>");
        Response.Write("<br />" + ex.ToString());

      }
    }

    public string AppSettings(string key)
    {
      return (string) System.Configuration.ConfigurationSettings.AppSettings[key];
    }

    public void DebugException(Exception ex)
    {
      Debug.WriteLine(String.Format("{0}: {1}\n{2}", DateTime.Now, ex.Message, ex.StackTrace));
    }

  }
}
Tags:
ASP.NET54 C#236 .NET66 Esempi225
Potrebbe interessarti anche: