Exposing Other Semantic Events The HitTracker Example


Exposing Other Semantic Events ”The HitTracker Example

So far, all the events that we implemented in controls are related to postback. However, your control can also expose events that are not related to postback. The HitTracker control that we'll now develop tracks page hits and raises an event related to the number of hits. Figure 9-11 shows this control.

Figure 9-11. HitTrackerTest.aspx viewed in a browser after an Announce ­Hits event is raised on the server

graphics/f09hn11.jpg

To reuse existing code, we'll derive HitTracker from the PageTracker control we developed in Chapter 7. We'll override the TrackingMode property so that HitTracker always tracks the total hits per page. We'll also expose a Multiple property and an AnnounceHits event. HitTracker raises the AnnounceHits event when the number of hits is divisible by the value of the Multiple property. For example, if a page developer assigns 100 to Multiple , HitTracker raises the AnnounceHits event when the number of hits is 100, 200, and so on. Listing 9-13 contains the code for HitTracker .

Listing 9-13 HitTracker.cs
 usingSystem; usingSystem.ComponentModel; usingSystem.Web.UI; usingSystem.Web.UI.WebControls; namespaceMSPress.ServerControls{ [DefaultEvent("AnnounceHits"), DefaultProperty("Multiple")] publicclassHitTracker:PageTracker{ privatereadonlyobjectEventAnnounceHits=newobject(); 
 [Bindable(true), DefaultValue("100")] publicintMultiple{ get{ objecto=ViewState["Multiple"]; return(o==null)?100:(int)o; } set{ if(value<1) thrownewArgumentOutOfRangeException("Multiplecannotbelessthan1"); ViewState["Multiple"]=value; } } [ Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), EditorBrowsable(EditorBrowsableState.Never) ] publicoverridePageTrackingModeTrackingMode{ get{ returnPageTrackingMode.ByApplication; } set{ if(value!=PageTrackingMode.ByApplication){ thrownewArgumentException("TrackingModemust"+ "equalPageTrackingMode.ByApplication."); } base.ViewState["TrackingMode"]=value; } } [Category("Default"), Description("RaisedwhenHitsisdivisiblebyMultiple.")] publiceventEventHandlerAnnounceHits{ add{ Events.AddHandler(EventAnnounceHits,value); } remove{ Events.RemoveHandler(EventAnnounceHits,value); } } protectedvirtualvoidOnAnnounceHits(EventArgse){ EventHandlerannounceHitsHandler= (EventHandler)Events[EventAnnounceHits]; if(announceHitsHandler!=null){ announceHitsHandler(this,e); } } protectedoverridevoidOnLoad(EventArgse){ base.OnLoad(e); if(Hits%Multiple==0) OnAnnounceHits(EventArgs.Empty); } } } 

Unlike postback events, which a control raises in the Raise Changed Events phase or the Raise Postback Event phase, you can raise an event that is not related to postback from any logical place in your control's code. In HitTracker , we raise the AnnounceHits event in the OnLoad method because the base class, PageTracker , sets the Hits property in OnLoad . HitTracker does not implement IPostBackDataHandler or IPostBackEventHandler because it does not participate in postback processing. In general, if you implement IPostBackDataHandler or IPostBackEventHandler , you might want to raise nonpostback events along with related postback events.

Listing 9-14 contains a page that uses the HitTracker control and attaches an event handler to its AnnounceHits event to announce prizes to visitors to the site. Figure 9-11 shows the page viewed in a browser.

Listing 9-14 HitTrackerTest.aspx
 <%@PageLanguage="C#"%> <%@RegisterTagPrefix="msp"Namespace="MSPress.ServerControls" Assembly="MSPress.ServerControls"%> <html> <head> <scriptrunat="server"> voidhitTracker1_AnnounceHits(objectsender,EventArgse){ label1.Text=String.Format("Congratulations!!!Asthe"+ "{0}thvisitortothissite,youhavewonaredMiata.", hitTracker1.Hits); label2.Text="Disclaimer:Thecarisaminiature."; } </script> </head> 
 <body> <formrunat="server"> <br> <asp:ButtonText="Submit"Runat="server"id="Button1"/> <br><br> <msp:HitTrackerid="hitTracker1"FormatString="PageHits:{0}" Width="200px"Font-Name="Verdana" Font-Size="14pt"ForeColor="Black" BackColor="#E0E0E0"runat="server" BorderStyle="None"BorderColor="White" Multiple="20" OnAnnounceHits="hitTracker1_AnnounceHits"/> <br> <asp:LabelFont-Size="14pt"Font-Bold="true"id="label1" runat="server"EnableViewState="False"/> <br> <asp:Labelid="label2"runat="server"EnableViewState="False"/> </form> </body> </html> 


Developing Microsoft ASP. NET Server Controls and Components
Developing Microsoft ASP.NET Server Controls and Components (Pro-Developer)
ISBN: 0735615829
EAN: 2147483647
Year: 2005
Pages: 183

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net