Home > ASP.NET > Ajax call before form submit asp.net c#

Ajax call before form submit asp.net c#

Let me share an issue i faced.

I have a textbox with google auto complete plugin.


I need to store the place i typed in, its latitude, longitude in to the database.

So google have the javascript api to get the latitude and longitude, but it should be called before the form button click to store that in Hidden fields(my way), so i can get the values in server side(c#).

I tried the OnClientClick, but the hidden fields are not set with lat and long. The form is always submitting before the result is came from the ajax call :(.

I found a solution.

var geocoder;
function SetLatLng() {
geocoder = new google.maps.Geocoder();
           var address1 = document.getElementById("location1").value;
geocoder.geocode({ 'address': address1 }, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
return false;
<asp:Button ID="FirstButton" OnClientClick="SetLatLng(); return false;" runat="server" Text="Click" />
<asp:Button ID="SecondButton" OnClick="buttonSubmit_Click" runat="server" Text="Click" style="display:none" />
<asp:HiddenField ID="HFLat" runat="server" Value="" />
<asp:HiddenField ID="HFLng" runat="server" Value="" />

Make 2 buttons, one for running the javascript, if the value is returned from the ajax call, click the second button programmatically 😛

Happy coding 🙂

  1. October 18, 2013 at 9:13 pm

    I’m very happy to discover this page. I wanted
    to thank you for ones time due to this fantastic read!!

    I definitely really liked every bit of it and I have you
    bookmarked to check out new information on your web site.

  2. February 26, 2014 at 5:25 pm

    In order to work 50 weeks a year, you’re going to have to
    work 30 weeks; in order to work 30 weeks, you’ll likely have to work 10.
    Think of this as a way to document your improvements over time, and eventually it will be a very
    effective PR tool. Whether these are taken live – while you are
    doing a real gig – or whether you have them done on a false stage, will
    depend on you.

  3. March 24, 2014 at 7:00 am

    Very good write-up. I absolutely love this website.

    Continue the good work!

  1. June 12, 2014 at 3:33 pm

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: