日期:2014-05-18  浏览次数:20395 次

自空义控件的问题
C# code

using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MSPress.ServerControl
{
    public enum PageTrackingMode
    {
        ByApplication,
        BySession,
        ByTripTime
    }
    [DefaultProperty("TrackingMode")]

    public class PageTracker : WebControl
    {
        private TimeSpan _tripTime;

        [Category("Appearance")]
        [DefaultValue("{0}")]
        [Description("The formatting string use to display value being tracked.")]
        public virtual string FormatString
        {
            get
            {
                string s = (string)ViewState["FormatString"];
                return ((s == null) ? "{0}" : s);
            }
            set
            {
                ViewState["FormatString"] = value;
            }
        }

        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public int Hits
        {
            get
            {
                PageTrackingMode mode = TrackingMode;
                object o = null;
                if (mode == PageTrackingMode.ByApplication)
                {
                    o = Page.Application[HitsKey];
                }
                else if (mode == PageTrackingMode.BySession)
                {
                    o = Page.Session[HitsKey];
                }
                else
                {
                    throw new NotSupportedException("Hits is only supported when TrackingMode is PageTrackingMode.ByApplication or PageTrackingMode.BySession");
                }
                return ((o == null) ? 0 : (int)o);
            }
        }

        [Category("Behavior")]
        [DefaultValue(PageTrackingMode.ByApplication)]
        [Description("Thy type of tracking to perform.")]
        public virtual PageTrackingMode TrackingMode
        {
            get
            {
                object mode = ViewState["TrackingMode"];
                return ((mode == null) ? PageTrackingMode.ByApplication : (PageTrackingMode)mode);
            }
            set
            {
                if (value < PageTrackingMode.ByApplication || value > PageTrackingMode.ByTripTime)
                {
                    throw new ArgumentOutOfRangeException("value");
                }
                ViewState["TrackingMode"] = value;
                switch (TrackingMode)
                {
                    case PageTrackingMode.ByApplication:
                        if (Page != null && Page.Application != null)
                        {
                            Page.Application.Remove(HitsKey);
                        }
                        break;
                    case PageTrackingMode.BySession:
                        if (Page != null && Page.Session != null)
                        {
                            Page.Session.Remove(HitsKey);
                        }
                        break;
                    case PageTrackingMode.ByTripTime:
                        ViewState.Remove("TimeStamp");
                        break;
                }
            }
        }

        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public TimeSpan TripTime
        {
            get
            {
                if (TrackingMode != PageTrackingMode.ByTripTime)
                {
                    throw new NotSupportedException("TripTime is only supported when TrackingMode is PageTrackingMode.ByTripTime");
                }
                return _tripTime;
            }
        }

        protected ov