Wednesday, March 8, 2017

How to track the user selected values in pages

As i discuss yesterday in which i track the time spend of the user on a single page there is a parameter variable as well .Please go to the link
https://engineerhacks.blogspot.ae/2017/03/how-to-measure-time-spent-on-page.html

so in parameter variable i need to track  what are the parameter user is selected  on pages.Currently i am working in MIS section so there are lots of reports. So I need to track  all the parameters that  generate report. So for now my requirements is to capture all the text boxes and drop downs values. you can modify this code as you requirements .

As always suggestion coming from seniors you can do it by passing in query string .... or you have to do one by one single pages because you don't know
the text boxes ids etc :)

I think differently  i think i can do this by using jquery confuse ???? :) . let me explain

Master page is perfect for this so when the user submit a button to generate reports it always input type="Submit" in my case i am lucky :)

so what i did i make a function that  find the click event for submit button and find all the fill text boxes and selected drop down values.

I also make
 <asp:HiddenField ID="hdn_parm" runat="server" /> 
 it will send the value to the server side


   <script type="text/javascript">
        var arr_txt = [];
        var arr_ddl = [];
 
        $(document).ready(function () {
            function BindValues() {
                $('input[type="text"]').each(function () {
                    if (this.value != '') {
                        var id = this.id;
                        id = id.split("_").pop();
                        arr_txt.push("ID: " + id + " : " + this.value);
                    }
                });

                $("select").each(function () {
                    var id = this.id;
                    id = id.split("_").pop();
                    if ($(this).find('option:selected').text() != '') {
                        arr_ddl.push("ID: " + id + " : " + $(this).find('option:selected').text());
                    }
                });

            }
         
            $('input[type="Submit"]').click(function (event) {
                BindValues();
             
                $("input[id*='hdn_parm']").val(" TextBox ID " + arr_txt.join(",") + " Drop Down ID " + arr_ddl.join(","));
                var parm_selection = $("input[id*='hdn_parm']").val();
                $.ajax({
                    url: 'pagename.aspx/parameters',
                    type: 'POST',
                    data: JSON.stringify({
                        parm_selection: parm_selection
                    }),
                    contentType: 'application/json; charset=utf-8',
                    dataType: "json",
                    success: function (response) {
                        //your success code
                    },
                    error: function (request, status, error) {
                       // console.log(request.responseText);
                     
                    }
                });
            });    
        });


Server Side :

[WebMethod(EnableSession = true)]
public static void parameters(string parm_selection)
{

// in parm_selection you can get all the selected drop downs and text boxes values here 

}




No comments:

Post a Comment