Reference http://www.cnblogs.com/bluewater/archive/2006/08/08/470093.html
Pagetracker is used to track the number of clicks received in the page session Status and program status, and calculate the page round-trip time.
Tracking Method:
public enum PageTrackingMode { ByApplicaion, BySession, ByTripTime }
1. Reference namespace
Using system;
Using system. componentmodel;
System. componentmodelThe namespace provides classes used to implement runtime and design-time behavior of components and controls. This namespace includes the base classes and interfaces used to implement attribute and Type converters, bind to data sources, and authorize components.
Using system. Web. UI;
System. Web. UIThe namespace provides classes and interfaces that allow you to create ASP. NET Server controls and pages that will appear as user interface elements in your web applications.
Http://msdn.microsoft.com/zh-cn/library/system.web.ui (vs.80). aspx
Using system. Web. UI. webcontrols;
System. Web. UI. webcontrolsThe namespace contains classes that can be used to create Web server controls on a webpage. The Web server control runs on the server and includes buttons, text boxes, and other form controls. They also include controls with special purposes (such as calendars ). Since Web server controls run on servers, these elements can be controlled programmatically. Although Web server controls are rendered as HTML, their object models do not necessarily reflect the HTML syntax.
System. Web. UI. webcontrolsThe namespace contains some HTML-tagged classes, such as textbox controls and ListBox controls. The namespace also contains some classes that do not present on the webpage but support data operations, such as the sqldatasource class and objectdatasource class. Other controls (such as the gridview control and detailsview Control) support data display and editing. Webcontrol class usedSystem. Web. UI. webcontrolsThe base classes of many classes in the namespace.
// Http://msdn.microsoft.com/zh-cn/library/system.web.ui.webcontrols (vs.80). aspx
2. trackingmode attribute Definition
For details about properties, see: http://msdn.microsoft.com/zh-cn/library/system.componentmodel (vs.80). aspx
[Defaultproperty ("trackingmode"), // specify the default attribute of the component] public class pagetracker: webcontrol // inherits from "webcontrol" {private timespan _ triptime; [category ("appearance"), // defaultvalue ("{0}") related to the appearance of the object, // specify the default value of the property. Description ("the formatting string used to display the" + "value being tracked.") // specifies the description of the property or event. ] // Exposes the formatstring attribute (read/write), which is a virtual reload and a public attribute.
Public Virtual string formatstring // virtual method, which can be rewritten. format String {get {string S = (string) viewstate ["formatstring"]; // return (S = NULL) from viewstate )? "{0}": S);} set
{Viewstate ["formatstring"] = value; // you can set the formatstring attribute .}}
3. Hits
[Browsable (false), // specifies whether an attribute or event should be displayed in the "attribute" window. Read-only attribute designerserializationvisibility (designerserializationvisibility. Hidden) // specify the persistence type used by the attribute (property) on the serialization component during design. // When the serialization program persists in the Design Pattern document, it usually adds code to the initialization method of the component to maintain the property value that has been set at design. // Hidden should not be kept in the initialization code, visible should be kept in the initialization Code]
// Expose the hits attribute (read-only) not virtual, public attribute public int hits {get {pagetrackingmode mode = trackingmode; // obtain the value of the property trackingmode object o = NULL; if (mode = pagetrackingmode. byapplicaion) // If the attribute is byapplication {o = page. application [hitskey];} else if (mode = pagetrackingmode. bysession) // If the attribute is bysession {o = page. session [hitskey];} else // attribute is other {Throw new notsupportedexception ("hits is only" + "supported when tracking Mode is "+" pagetrackingmode. byapplication or "+" pagetrackingmode. bysession ");} return (O = NULL )? 0: (INT) O); // returned property value }}
4. Set and get of the virtual Behavior Method
[Category ("behavior"), // defaultvalue (pagetrackingmode. byapplicaion), // The default attribute value is byapplication description ("the type of tracking to perform. ") // category description] // exposes the trackingmode attribute (read/write), which is a virtual overload and a public attribute.
Public Virtual pagetrackingmode trackingmode // defines the virtual method. The returned value is of the enumeration type {get {object mode = viewstate ["trackingmode"]; // get the value return (mode = NULL) from viewstate )? Pagetrackingmode. byapplicaion: (pagetrackingmode) mode); // if the property value is "null", the application is used for storage. If it is not empty, returns the property value obtained from viewstate} set // sets the property value {If (value <pagetrackingmode. byapplicaion | value> pagetrackingmode. bytriptime) {Throw new argumentoutofrangeexception ("value");} viewstate ["trackingmode"] = value; // execute clearing the old storage // we need to check that the page is not empty // because the control may be initialized before it is added to the Control tree. // Note: Application and session objects are not available in the design view, so an exception switch (Track Ingmode) {Case pagetrackingmode. byapplicaion: // if you use application if (page! = NULL & page. application! = NULL) // If the page and page. application is not blank {page. application. remove (hitskey); // remove application} break named hitskey; Case pagetrackingmode. bysession: // if you use session if (page! = NULL & page. session! = NULL) {page. session. remove (hitskey); // removes the session} break named hitskey; Case pagetrackingmode. bytriptime: // If viewstate is used. remove ("timestamp"); // remove the viewstate break named timestamp ;}}}
5. The properties of triptime, tagkey, and hitskey are not displayed in the Properties window. No persistent Properties
[Browsable (false), designerserializationvisibility (designerserializationvisibility. Hidden) // read-only attribute] // expose the triptime attribute (read-only) not virtual, public attribute
public TimeSpan TripTime { get { if (TrackingMode != PageTrackingMode.ByTripTime) { throw new NotSupportedException("TripTime is " + "only supported when TrackingMode is " + "PageTrackingMode.ByTripTime "); } return _tripTime; } }//http://msdn.microsoft.com/zh-cn/library/system.web.ui.htmltextwritertag(VS.80).aspx
Protected override htmltextwritertag tagkey {get {return htmltextwritertag. div ;}} private string hitskey {get {// create a unique hitskey for the page for keying in // hits per page in the application an session objects // each time you click the button on the page ... return page. getType (). fullname ;}// update the status of the hits and triptime attributes
Protected override void onload (eventargs e) {base. onload (E); Switch (trackingmode) {Case pagetrackingmode. byapplicaion: Lock (page. getType () {// because the application can be accessed by multiple visitors at the same time, the page is locked. application [hitskey] = hits + 1;} break; Case pagetrackingmode. bysession: page. session [hitskey] = hits + 1; break; Case pagetrackingmode. bytriptime: Object timestamp = viewstate ["timestamp"]; // obtain the time of the previous request // (the time stored in "viewstate") datetime requesttime = context. timestamp; // obtain the server time if (timestamp = NULL) {_ triptime = timespan. zero; this. visible = false;} else {This. visible = true; _ triptime = (requesttime-(datetime) timestamp); // round-trip time} viewstate ["timestamp"] = requesttime; // set viewstate ["timestamp"] to the server time break;} protected override void rendercontents (htmltextwriter writer) {Switch (trackingmode) {Case pagetrackingmode. byapplicaion: Case pagetrackingmode. bysession: writer. write (formatstring, hits); break; Case pagetrackingmode. bytriptime: writer. write (formatstring, triptime. totalseconds); break ;}}