Click here to Skip to main content
15,892,161 members
Please Sign up or sign in to vote.
2.33/5 (2 votes)
See more:
Hello all,
i am trying to get JavaScript variable to hidden field value and store in asp.cs page.
but after post back it again and again call button.click function.how to prevent this..

button click is $("#btnAfterLoad").click();
function is BindUserInfo()

What I have tried:

C#
<script type="text/javascript">
        $(document).ready(function () {

            if (navigator.geolocation) {
                navigator.geolocation.getCurrentPosition(success);
                function success(position) {
                  
                    var lat, long;
                    lat = position.coords.latitude;
                    long = position.coords.longitude;

                    //var city = position.coords.locality;
                    //$(document).ready(function () {
                    //    $('#hdnlat').val(lat);
                    //    alert($('#hdnlat').val());
                    //    $('#hdnlng').val(long);
                    //    alert($('#hdnlng').val());
                    //});
                    $(function () {
                        BindUserInfo();

                        $("#btnAfterLoad").click();
                       
                    })
                    function BindUserInfo() {
                        $(<%=hdnlat.ClientID%>).val(lat);
                        //document.getElementById('hdnlat').innerHTML = lat;
                        alert($('#hdnlat').val());
                        $('#hdnlng').val(long);
                        //document.getElementById('hdnlng').innerHTML = lng;
                        alert($('#hdnlng').val());


                        
                    }
                }
            };

        });

    </script>


 <asp:Button runat="server" ID="btnAfterLoad" OnClick="btnAfterLoad_Click" style="display:none" />
Posted
Updated 17-Jun-16 6:04am
v2
Comments
Sinisa Hajnal 17-Jun-16 2:30am    
Don't call btn click after BindUserInfo. You basically told the application, after you are ready, bind user info and call button click (which will trigger round trip to server, do whatever it is supposed to do, then re-load the page. This will trigger .ready event which will call BindUserInfo and click the button that will initiate ANOTHER server round trip ad infinitum.
Member 10852141 17-Jun-16 3:11am    
heloo sinisa,

give me on eexample...like demo..
Vincent Maverick Durano 17-Jun-16 10:23am    
There's no need. You just need to remove this line

$("#btnAfterLoad").click();

You can then get the value of the HiddenField at your Button's click event at the server.

remove this line

JavaScript
$(function () {
BindUserInfo();

$("#btnAfterLoad").click();

})


If you want to Post the data to the server use Ajax[^]
 
Share this answer
 
To stop the postback in future you do two things: one is the code below preventing the default event of the button, the second is returning false on an onclientclick

function eventFunction(e)
{
e.preventDefault();
//events to follow after event with
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900