2

I wanna to track the user activity (log data). i have several links on the page which submit form through specific way .the problem is :

I can't find a suitable way to handle the click event and insert the log in the database in a simple way.

My code :

HtmlGenericControl a = new HtmlGenericControl("a");
a.Attributes["onclick"] = "$('#" + frm.ClientID + "').submit();";
a.InnerText = "site " + dt_list.ElementAtOrDefault(0).Field<string>("pro_name").TrimEnd();
inner_li_1.Controls.Add(a);

Now i wanna to handle the click event of the link which make the submission ?!!

gdoron
  • 147,333
  • 58
  • 291
  • 367
Anyname Donotcare
  • 11,113
  • 66
  • 219
  • 392
  • 3
    Handle client-side scripts in the code-behind is definitely not a simple way. I believe I told you it twice in the past... – gdoron Jun 10 '12 at 12:38
  • :) :) could u suggest an answer ,cause i think handling it client side then store the log in DB is more complicated !! – Anyname Donotcare Jun 10 '12 at 12:39
  • If you can show us the **rendered** HTML, and what you want to do with it, I could answer. – gdoron Jun 10 '12 at 12:45
  • `` Now ,i wanna to insert log info in my database when the user click on that link.. My code in the question is dynamic to generate several links for several sites depending on some paramaerters,so i want to insert log for every click on any link. – Anyname Donotcare Jun 10 '12 at 12:50
  • @gdoron is correct. Practice unobtrusive JavaScript and your development life will be exponentially easier. Keep your js separate from your markup. Assign an id to your link, add the click event handler to it after the DOM loads, and use $("form [id$='theformid']).submit() to submit the form. – Mad Man Moon Jun 10 '12 at 14:22

2 Answers2

2

This is too much, how ever its works, and I use it in some case that I can not do other way.

The javascript call, I call the log first, then continue with submit. I have also show a wait message when I make the click, how ever the events happends too fast and the user did not understand that is wait for anything. I also take care to avoid double log of the action with simple javascript. I call this function on the onclick event.

var confirmSubmited = false;
function SubmitWithLog(me)
{
    // to avoid many clicks...
    if(confirmSubmited)
      return false;

    confirmSubmited=true;

    jQuery.ajax({
        url: "/LogAction.ashx",
        type: "GET",
        timeout: 3000,
        async: true, // you can try and async:false - maybe is better for you
        data: action=4, // here you send the log informations
        cache: false,
        success: function(html) {
            jQuery("#FormID").submit();
        },
        error: function(responseText, textStatus, XMLHttpRequest) {                 
            jQuery("#FormID").submit();
        }
    });

    return false;
}

The handler is as

public class LogAction : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
                // log here what you wish

                // end up with no content
        context.Response.TrySkipIisCustomErrors = true;
        context.Response.Status = "204 No Content";
        context.Response.StatusCode = 204;          
    }
}

I like to clear that this is not a good way to log the user action, but its a way when you can not do otherwise. I use it only ones, when the user make submit and leave my site and go to one other site, and I use it to write this action.

In all other cases I use ether a reference in the url, ether make log on code behind, ether I include an empty image in the html page that make the log like that: ASP server stats for html pages

I have try to call ajax, and right way make the submit, but its fails, the ajax is stop by submit. The next way was to use timeout, to call ajax, and set timeout 500ms to make the submit, but I avoid the timeout because the ajax call takes less than 100ms as I have made it.

Community
  • 1
  • 1
Aristos
  • 66,005
  • 16
  • 114
  • 150
  • How to use Submitwithlog to save the log in my database !! I use the following method in my code : `base.TraceActivity(".....", "https://" + Request.Url.Authority + "/LoginPage.aspx", statistics[0], statistics[1], statistics[2]);` – Anyname Donotcare Jun 10 '12 at 12:55
  • @just_name You make a handler called `LogAction.ashx` and in there you make the log... Actually what I do here is to call a handler before make the submition. – Aristos Jun 10 '12 at 12:57
  • 1
    @just_name Its is not so simple, is a little complicate, from the other hand if you understand it is not so complicate :) and its works. – Aristos Jun 10 '12 at 12:59
  • hmmm when i try this with `firefox` and `google chrome` it doesn't work ,the `onclick` event doesn't fire !!.it works only with `IE` – Anyname Donotcare Jun 14 '12 at 10:05
  • 1
    @just_name Its work for sure on all browsers. Maybe you need to add on href='#' or something similar, make a search on internet on how to call javascript from anchor for the details. – Aristos Jun 14 '12 at 10:08
  • `GO` this's from firebug – Anyname Donotcare Jun 14 '12 at 10:10
  • @just_name because is 'a' maybe its need a default `href` add it, with some parametre like `#` – Aristos Jun 14 '12 at 10:10
  • @just_name read this http://stackoverflow.com/questions/134845/href-tag-for-javascript-links-or-javascriptvoid0 – Aristos Jun 14 '12 at 10:13
  • thanks a lot , but i do this : `GO ` and still doesn't work – Anyname Donotcare Jun 14 '12 at 10:23
  • yes `Sys.ParameterCountException: Parameter count mismatch. [Break On This Error]{name: "format", type: String}` – Anyname Donotcare Jun 14 '12 at 10:35
  • 1
    @just_name this is something diferent, I do not know if they have any conflict together, maybe you need to use the jquery no conflict. Also read this http://wraithnath.blogspot.gr/2012/03/error-sysparametercountexception.html – Aristos Jun 14 '12 at 10:38
  • really thanks but when i do this i get another error `this._toFormattedString is not a function` – Anyname Donotcare Jun 14 '12 at 11:00
  • I post a question with more details :) – Anyname Donotcare Jun 14 '12 at 11:59
1

@Aristos -- Why in the world would you negatively impact the user experience by waiting for the Ajax call that does the logging to return before moving on with the event (click, submit, whatever...)?!? Logging is a non-critical part of the application, where the user experience does not change based on its results. It's a "send and forget" type routine. Fire off the Ajax request, leave the success and failure routines empty, and execute the submit on the line immediately following the $.ajax(); call.

So, some pseudo code for you...

function handleClick(e) {
    handleLoggingOnClick("something happened here");

    myForm.submit();
}

function handleLoggingOnClick(message) {
    $.ajax({url:"logging.do", data:"message="+message});  
}
Mad Man Moon
  • 741
  • 4
  • 10
  • Because I have try that, and its fail to make the log because the submit is stop the ajax. My code is from real word web... I mean I use it on my page with real users and tested. – Aristos Jun 10 '12 at 22:09
  • I like to clear that this way of log is not for used in too many pages, and a simple different way must be used - a total different design. How ever there are some rare cases that this is the only way. I used it to log only one click, only in one page in my program. – Aristos Jun 10 '12 at 22:14