Where is your initialize(); function? Can you post it? I think there is a problem with that function. Also check your browser console for errors when you click on the button. Without seeing what is inside that javascript function, we cannot tell what's
the problem. Syntaxtically, your code is correct. If you are referencing an external JS file, verify the path is correct.
function initialize() {
geocoder = new google.maps.Geocoder(); // creating a new geocode object
// getting the two address values
address1 = document.getElementById("address1").value;
address2 = document.getElementById("address2").value;
// finding out the coordinates
if (geocoder) {
geocoder.geocode({ 'address': address1 }, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
//location of first address (latitude + longitude)
location1 = results[0].geometry.location;
var state = "N/A";
for (var component in results[0]['address_components']) {
for (var i in results[0]['address_components'][component]['types']) {
if (results[0]['address_components'][component]['types'][i] == "administrative_area_level_1") {
state = results[0]['address_components'][component]['short_name'];
// do stuff with the state here!
document.getElementById('lblState').innerHTML=state;
}
}
}
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
geocoder.geocode({ 'address': address2 }, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
//location of second address (latitude + longitude)
location2 = results[0].geometry.location;
// calling the showMap() function to create and show the map
showMap();
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
}
}
this is my javascript function and its working on click event of an HTML button but not on Asp.net button
When you want to execute the JavaScript function. If you want to execute the JS function after the button click server side event, you should not use onclientclick. In that case try
If you want to execute the JS function before the server side click event, you should be carefull because all the javascript modification done will be lost unless you persist them in form element. Like you want to persist something add the value to hidden
field and reuse after postback.
Thanks & Regards
Anup Das Gupta
Mark as Answer if you feel so. Visit My Blog
Marked as answer by singh1030 on Apr 08, 2012 07:55 AM
singh1030
0 Points
6 Posts
on client click not workin
Apr 07, 2012 03:13 PM|LINK
In this "onclick" is working but it is not calling javascript function. please help how to execute both onclick and onclientclick at the same time.
<asp:Button ID="btnFare" OnClientClick="return initialize();" runat="server" Text="Calculate Fare" onclick="btnFare_Click"/>
please suggest how to deal with this javascript problem in asp.net.
Thanks in advance
javascript
avinash_bhud...
Contributor
2881 Points
517 Posts
Re: on client click not workin
Apr 07, 2012 03:39 PM|LINK
You have to return true from your "OnClientClick" javscript function in order to get server side click event fired.
Modify your code as below.
<asp:Button ID="btnFare" OnClientClick="initialize();return true;" runat="server" Text="Calculate Fare" onclick="btnFare_Click"/>
Hope it helps.
javascript
singh1030
0 Points
6 Posts
Re: on client click not workin
Apr 07, 2012 04:01 PM|LINK
yes but the problem it is executing server side function only and javascript function is not executed after the button click .
I want to execute both and that time its not working
javascript
Ruchira
All-Star
44216 Points
7184 Posts
MVP
Re: on client click not workin
Apr 07, 2012 04:24 PM|LINK
Hello,
Where is your initialize(); function? Can you post it? I think there is a problem with that function. Also check your browser console for errors when you click on the button. Without seeing what is inside that javascript function, we cannot tell what's the problem. Syntaxtically, your code is correct. If you are referencing an external JS file, verify the path is correct.
javascript
My Tech blog | My YouTube ChannelPlease 'Mark as Answer' if this post helps you.singh1030
0 Points
6 Posts
Re: on client click not workin
Apr 07, 2012 05:10 PM|LINK
function initialize() {
geocoder = new google.maps.Geocoder(); // creating a new geocode object
// getting the two address values
address1 = document.getElementById("address1").value;
address2 = document.getElementById("address2").value;
// finding out the coordinates
if (geocoder) {
geocoder.geocode({ 'address': address1 }, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
//location of first address (latitude + longitude)
location1 = results[0].geometry.location;
var state = "N/A";
for (var component in results[0]['address_components']) {
for (var i in results[0]['address_components'][component]['types']) {
if (results[0]['address_components'][component]['types'][i] == "administrative_area_level_1") {
state = results[0]['address_components'][component]['short_name'];
// do stuff with the state here!
document.getElementById('lblState').innerHTML=state;
}
}
}
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
geocoder.geocode({ 'address': address2 }, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
//location of second address (latitude + longitude)
location2 = results[0].geometry.location;
// calling the showMap() function to create and show the map
showMap();
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
}
}
this is my javascript function and its working on click event of an HTML button but not on Asp.net button
<input type="button" value="Show" onclick="initialize();"/>
it is working for above html button. but not for this
<asp:Button ID="btnFare" OnClientClick=" return initialize();" runat="server" Text="Calculate Fare" onclick="btnFare_Click"/>
Ruchira
All-Star
44216 Points
7184 Posts
MVP
Re: on client click not workin
Apr 07, 2012 05:13 PM|LINK
Here you are NOT returning.
But you here you are returning. I don't see why you should return here. Just remove the return and try again as below
My Tech blog | My YouTube ChannelPlease 'Mark as Answer' if this post helps you.singh1030
0 Points
6 Posts
Re: on client click not workin
Apr 07, 2012 05:18 PM|LINK
i have tried that also that is also not working.
asteranup
All-Star
30184 Points
4906 Posts
Re: on client click not workin
Apr 08, 2012 06:25 AM|LINK
Hi,
When you want to execute the JavaScript function. If you want to execute the JS function after the button click server side event, you should not use onclientclick. In that case try
http://delicious.com/anupdg/RegisterStartupScript
If you want to execute the JS function before the server side click event, you should be carefull because all the javascript modification done will be lost unless you persist them in form element. Like you want to persist something add the value to hidden field and reuse after postback.
Anup Das Gupta
Mark as Answer if you feel so. Visit My Blog
singh1030
0 Points
6 Posts
Re: on client click not workin
Apr 08, 2012 07:56 AM|LINK
thanks for your help.
I register the javascript and i changed the html textbox to asp textbox and
it works
singh1030
0 Points
6 Posts
Re: on client click not workin
Apr 09, 2012 07:26 AM|LINK
now one more probelm is coming that i need to fire javascript function first and then server side function.
<asp:Button ID="btnFare" runat="server" Text="Calculate Fare" onclick="btnFare_Click"/> protected void btnFare_Click(object sender, EventArgs e) { ClientScript.RegisterStartupScript(GetType(), "Javascript", "javascript:initialize(); ",true); double farecharges; double distance; distance=Convert.ToDouble(lblDistance.Value); }here i am getting lbldistance value from javascript it is showing error because javascript function is firing after server side event
what should i do so that javascript function should fire first.