.

pagebase

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.IO;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public class BasePage : System.Web.UI.Page
{
    #region Overridden Page Events
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);

        // Start measuring page execution time
        if (this.MeasureExecutionTime)
        {
            watch = new Stopwatch();
            watch.Start();

            this.RecordTimestamp("Page execution starting...");
        }
    }

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

        // Set the page title
        this.SetPageTitle();
    }

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

        if (this.MeasureExecutionTime)
        {
            this.RecordTimestamp("Page execution complete...");

            watch.Stop();

            Control addExecutionTo = this.Form;
            if (addExecutionTo == null)
                addExecutionTo = this;

            addExecutionTo.Controls.Add(new LiteralControl("<br /><br />"));
           
            StringBuilder timestampsOutput = new StringBuilder(50 * this.ExecutionTimestamps.Count);
            for (int i = 0; i < this.ExecutionTimestamps.Count; i++)
                timestampsOutput.AppendFormat("<b>Timestamp {0}</b>: {1:N0} ms ({2})<br />{3}",
                                                i + 1, this.ExecutionTimestamps[i].Timestamp,
                                                this.ExecutionTimestamps[i].Description, Environment.NewLine);

            addExecutionTo.Controls.Add(new LiteralControl(timestampsOutput.ToString()));
        }
    }
    #endregion

    #region DisplayAlert
    protected virtual void DisplayAlert(string message)
    {
        ClientScript.RegisterStartupScript(
                        this.GetType(),
                        Guid.NewGuid().ToString(),
                        string.Format("alert('{0}');", message.Replace("'", @"\'")),
                        true
                    );
    }
    #endregion

    #region FindControlRecursive
    protected virtual Control FindControlRecursive(string id)
    {
        return FindControlRecursive(id, this);
    }

    protected virtual Control FindControlRecursive(string id, Control parent)
    {
        // If parent is the control we're looking for, return it
        if (string.Compare(parent.ID, id, true) == 0)
            return parent;
       
        // Search through children
        foreach (Control child in parent.Controls)
        {
            Control match = FindControlRecursive(id, child);

            if (match != null)
                return match;
        }

        // If we reach here then no control with id was found
        return null;
    }
    #endregion

    #region SetPageTitle
    protected virtual bool AutoSetPageTitle
    {
        get
        {
            object o = ViewState["AutoSetPageTitle"];
            if (o == null)
                return true;
            else
                return (bool)o;
        }
        set
        {
            ViewState["AutoSetPageTitle"] = value;
        }
    }

    protected virtual string SiteMapProvider
    {
        get
        {
            string str = ViewState["SiteMapProvider"] as string;
            if (str == null)
                return string.Empty;
            else
                return str;
        }
        set
        {
            ViewState["SiteMapProvider"] = value;
        }
    }

    protected virtual string SiteMapNodeSeparator
    {
        get
        {
            string str = ViewState["SiteMapNodeSeparator"] as string;
            if (str == null)
                return " :: ";
            else
                return str;
        }
        set
        {
            ViewState["SiteMapNodeSeparator"] = value;
        }
    }

    protected virtual void SetPageTitle()
    {
        if (this.AutoSetPageTitle && (string.IsNullOrEmpty(this.Title) || string.Compare(this.Title, "Untitled Page", true) == 0))
        {
            if (SiteMap.Enabled)
            {
                // See if this page is in the SiteMap
                SiteMapNode current = null;

                if (string.IsNullOrEmpty(this.SiteMapProvider))
                    current = SiteMap.CurrentNode;
                else
                    current = SiteMap.Providers[this.SiteMapProvider].CurrentNode;

                if (current != null)
                {
                    // Build up the title
                    SetPageTitle(current);
                    return;
                }
            }

            // If we reach here we still do not have a title set... use the filename
            this.Title = Path.GetFileNameWithoutExtension(Request.PhysicalPath);
        }
    }

    protected virtual void SetPageTitle(SiteMapNode current)
    {
        if (this.AutoSetPageTitle)
        {
            StringBuilder titleBuilder = new StringBuilder(200);
            titleBuilder.Append(current.Title);

            current = current.ParentNode;

            if (current != null)
            {
                string parentPathReverse = current.Title;

                while (current.ParentNode != null)
                {
                    current = current.ParentNode;

                    parentPathReverse = string.Concat(current.Title, this.SiteMapNodeSeparator, parentPathReverse);
                }

                titleBuilder.Append(" (").Append(parentPathReverse).Append(")");
            }

            this.Title = titleBuilder.ToString();
        }
    }
    #endregion

    #region ExecutionTime
    protected virtual bool MeasureExecutionTime
    {
        get
        {
            object o = ViewState["MeasureExecutionTime"];
            if (o == null)
                return false;
            else
                return (bool)o;
        }
        set
        {
            ViewState["MeasureExecutionTime"] = value;
        }
    }

    private Stopwatch watch = null;

    private List<TimestampInfo> timestamps = null;
    protected virtual List<TimestampInfo> ExecutionTimestamps
    {
        get
        {
            if (timestamps == null)
                timestamps = new List<TimestampInfo>();

            return timestamps;
        }
    }

    protected virtual void RecordTimestamp(string description)
    {
        if (watch == null)
            throw new ArgumentException("In order to record page execution time MeasureExecutionTime must be set to True.");

        ExecutionTimestamps.Add(new TimestampInfo(watch.ElapsedMilliseconds, description));
    }
    #endregion
}

One Response to this post.

  1. Posted by dotNetFollower on 21.05.10 at 11:00 am

    Nice post! My own FindControlRecursive helped me out many times. Especially, when I deal with MasterPages. For example, one of such usage is shown in my post here – http://dotnetfollower.com/wordpress/2010/12/sharepoint-add-onchange-attribute-to-dropdownchoicefield/.
    Thank you!

What's your thoughts on this?

*

Protected by WP Anti Spam