Adding Comment Tracking Automatically

Now that you've seen the basics of HttpModules, the question arises, How do you change the data in an aspx page? When I first thought about this I looked for an output stream that I could parse and change, but I could not find one anywhere. I approached this problem later, after writing a few custom controls, and it was almost too obvious. You don't have access to an output stream. Instead, you have access to the Controls collection of the page. So to add our comment tracking control to the page, it is enough to search through the Controls collection until you find a good place to add the control.

Listing 15.8 shows the complete HttpModule to automatically add the comment control to the page.

Listing 15.8 Auto-Comment Module AutoCommentModule.cs
 using System; using System.Configuration; using System.Collections.Specialized; using System.Web; using System.Web.UI; namespace Comment {     public class AutoCommentModule : IHttpModule     {         public AutoCommentModule()         {         }         // IHttpModule Interface         public void Init(HttpApplication objApplication)         {             objApplication.PreRequestHandlerExecute+=                 new EventHandler(OnPreRequestHandlerExecute);         }          public void Dispose()         {         }         public void OnPreRequestHandlerExecute(Object objSender,             EventArgs objArgs)         {             HttpApplication objApplication=                 (HttpApplication) objSender;             Page objPage=(Page) objApplication.Context.Handler;             objPage.Init+=new EventHandler(OnPageInit);         }         public void OnPageInit(Object objSender,EventArgs objArgs)         {             Page objPage=(Page) objSender;             // Create our Comment Control             CommentCtl objCommentCtl=new CommentCtl();             LoadConfigSettings(objCommentCtl);             objCommentCtl.;             if(objPage.Session["Reload"]!=null)             {                 if((bool)objPage.Session["Reload"]==true)                 {                     objPage.Controls.Clear();                     objPage.Controls.Add(objCommentCtl);                     return;                 }             }             // Locate the position to add it             System.Web.UI.ControlCollection objControls=                 objPage.Controls;             // Assume that the Form is a top level             // control in the page.             for(int i=0;i<objControls.Count;++i)             {                 if(objControls[i].GetType()==                     typeof(System.Web.UI.HtmlControls.HtmlForm))                 {                     // Add the control                     objControls[i].Controls.                         AddAt(0,objCommentCtl);                     return;                 }             }         }         private void LoadConfigSettings(CommentCtl objComment)         {             NameValueCollection objConfigData =                 ConfigurationSettings.GetConfig("CommentCtl")                 as NameValueCollection;             for(int i=0;i<objConfigData.Count;++i)             {                 String sKey=objConfigData.GetKey(i);                 if(sKey == "ConnectionString")                     objComment.ConnectionString=objConfigData[i];                 else if(sKey == "Revision")                     objComment.Revision=                         Convert.ToInt32(objConfigData[i]);                 else if(sKey == "CssFile")                     objComment.CssFile=objConfigData[i];             }         }     } } 

One thing to remember about the comment control is that it does post backs to the server. This is important because it means that the comment control needs to be added inside the Form control that most aspx pages contain.

At first I thought that I wanted it to be added at the end of the last Form control on the page, but after trying that, I found that the page would appear to render, and then after a slight pause the comment control would appear. This was quite annoying, so I moved the comment control to the beginning of the first Form control on the page.

Application Settings

Because this module was intended to be used in a variety of settings, configuration data was placed in a custom section of the web.config file. The following entries need to be made inside the <configuration> section:

 <configSections>   <section name="CommentCtl"     type="System.Configuration.NameValueSectionHandler,     system, Version=1.0.3300.0, Culture=neutral,     PublicKeyToken=b77a5c561934e089, Custom=null" /> </configSections> <CommentCtl>   <add     key="ConnectionString"     value='Provider=Microsoft.Jet.OLEDB.4.0;Password="";       User ID=Admin;       Data Source=C:\FullPath\comments.mdb' />   <add key="Revision" value="1" />   <add key="CssFile" value="/relativepath/CommentStyle.css" /> </CommentCtl> 

Additionally, to activate the HttpModule, the following entries need to be made inside the <system.web> section of the web.config file:

 <httpModules>   <add name="AutoComment"   type="Datasphere.Development.WebUI.AutoCommentModule,    CommentCtl" /> </httpModules> 

ASP. NET Solutions - 24 Case Studies. Best Practices for Developers
ASP. NET Solutions - 24 Case Studies. Best Practices for Developers
ISBN: 321159659
Year: 2003
Pages: 175 © 2008-2017.
If you may any questions please contact us: