locked
Using HttpHandlers/HttpModule to track button clicks RRS feed

  • Question

  • User1191616196 posted

     Hi,

    I'm facing an issue implementing a tracking feature using HttpHandlers that would allow me to identify which button was clicked. Below is the code that I've written for this purpose.

    public void Init(HttpApplication context)
            {
                context.PreRequestHandlerExecute += new EventHandler(EventHandler);
            }

    public void EventHandler(object sender, EventArgs e)
            {
                HttpApplication application = sender as HttpApplication;
                IHttpHandler handler = application.Context.Handler;


                if (handler is Button)
                {
                    ((Button)handler).Click += new EventHandler(ButtonClickEvent);
                }
     
            }

     

    The issue that I'm facing is that the "if" condition never satisfies and hence never enters. I need to identify which button was clicked using some property of the button say, ID. Kindly let me know how I can make this possible.

    Also, can I implement the same logic to track HTML hyperlinks tat are clicked. Since there won't be an event associated with a HTML hyperlink. I would like to know how I can go about this too.

    Thanks in advance.

    Regards,

    Frenz

    Wednesday, August 26, 2009 2:30 AM

Answers

  • User1142054445 posted

     check if this works...

    public class Class1:IHttpModule
        {
            #region IHttpModule Members
    
            public void Dispose()
            {
                throw new NotImplementedException();
            }
    
            public void Init(HttpApplication context)
            {
                context.PreRequestHandlerExecute += new EventHandler(context_PreRequestHandlerExecute);
            }
    
            void context_PreRequestHandlerExecute(object sender, EventArgs e)
            {
                HttpApplication app = (HttpApplication)sender;
                System.Web.UI.Page page = (System.Web.UI.Page)app.Context.CurrentHandler;
                page.PreRender += new EventHandler(page_PreRender);                        
            }
    
            void page_PreRender(object sender, EventArgs e)
            {
                HtmlForm form = ((Page)sender).Form;
                foreach (Control ctl in form.Controls)
                {
                    if (ctl.GetType().ToString() == "System.Web.UI.WebControls.Button")
                    {
                        ((Button)ctl).Click += new EventHandler(Button1_Click);
                    }
                }
            }
    
            public void Button1_Click(object sender, EventArgs e)
            {
                Console.WriteLine("hello");
            }
            #endregion


     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 28, 2009 9:26 AM
  • User-1136466523 posted

    Hi,

    From your code, your handler cannot be converted to Page object. 

    For example, it only works in the following scenario.

    "public class Page : TemplateControl, IHttpHandler{...}"

    Thanks.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 31, 2009 11:30 PM

All replies

  • User1142054445 posted

     Tip: You may use context.request.form.Allkeys. If a button is clicked you will find the button ID as a key..

    Thursday, August 27, 2009 1:35 AM
  • User1191616196 posted

    Thanks. Now I'm able to get the ID's. Going one step further, how can I make use of this code:

    ((Button)handler).Click += new EventHandler(ButtonClickEvent);

    So that when any button is clicked the event "ButtonClickEvent" in HttpModule is fired. This would help me avoid reading the list of keys each time and compare the button ID. Also, this is a problem for me since I also have some dynamically generated buttons.


    Also, any idea on tracking clicks on HTML hyperlink?

    Thursday, August 27, 2009 9:04 AM
  • User1142054445 posted

     check if this works...

    public class Class1:IHttpModule
        {
            #region IHttpModule Members
    
            public void Dispose()
            {
                throw new NotImplementedException();
            }
    
            public void Init(HttpApplication context)
            {
                context.PreRequestHandlerExecute += new EventHandler(context_PreRequestHandlerExecute);
            }
    
            void context_PreRequestHandlerExecute(object sender, EventArgs e)
            {
                HttpApplication app = (HttpApplication)sender;
                System.Web.UI.Page page = (System.Web.UI.Page)app.Context.CurrentHandler;
                page.PreRender += new EventHandler(page_PreRender);                        
            }
    
            void page_PreRender(object sender, EventArgs e)
            {
                HtmlForm form = ((Page)sender).Form;
                foreach (Control ctl in form.Controls)
                {
                    if (ctl.GetType().ToString() == "System.Web.UI.WebControls.Button")
                    {
                        ((Button)ctl).Click += new EventHandler(Button1_Click);
                    }
                }
            }
    
            public void Button1_Click(object sender, EventArgs e)
            {
                Console.WriteLine("hello");
            }
            #endregion


     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 28, 2009 9:26 AM
  • User1191616196 posted

    Hi,

    I tried the code. I'm getting the error "Unable to cast object of type 'System.Web.Handlers.AssemblyResourceLoader' to type 'System.Web.UI.Page'." in the following line of context_PreRequestHandlerExecute method:

    System.Web.UI.Page page = (System.Web.UI.Page)app.Context.CurrentHandler;


    Please let me know what can be done.


    Thanks,
    Frenz

     

    Friday, August 28, 2009 10:48 AM
  • User-1136466523 posted

    Hi,

    From your code, your handler cannot be converted to Page object. 

    For example, it only works in the following scenario.

    "public class Page : TemplateControl, IHttpHandler{...}"

    Thanks.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 31, 2009 11:30 PM