Click here to Skip to main content
15,887,175 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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.

XML
<asp:Button ID="btnFare"  OnClientClick="return initialize();"  runat="server"  Text="Calculate Fare" onclick="btnFare_Click"/>
please suggest solution of this problem.


Thanks in advance
Posted
Updated 7-Apr-12 5:39am
v2
Comments
Uday P.Singh 7-Apr-12 11:41am    
can you show us the Javascript code?, as there is very little info. to give a proper solution
Member 8150795 7-Apr-12 13:13pm    
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" önclick="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"/>

Hi,
Take a look at this:
http://forums.asp.net/t/1627090.aspx[^]

I hope it helps
 
Share this answer
 
Its not possible to call both the click event at same time . Here 1st client onclick will be executed (OnClientClick) the return of javascript also effects on it . If the return type is false from the javascript then the "onclick" event does not execute it .So check weather the javascript returns true in order to execute the server return click (onclick).
 
Share this answer
 
Comments
Member 8150795 7-Apr-12 12:05pm    
but i need both function to be executed after button click but it is executing

server side function only and return type is true only...
Member 8150795 7-Apr-12 13:13pm    
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" önclick="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"/>
OnClientClick click is called first and if it return true than OnClick is called.

if you want data from OnClientClick event after OnClieck is called than store your Data into hiddent field and render it on OnClick event than again Display it in OnClick Server side event.

Hope it will work for you.
 
Share this answer
 
Comments
Member 8150795 11-Apr-12 8:38am    
yes i have been trying like this
ClientScript.RegisterStartupScript(GetType(), "Javascript", "javascript:initialize(); ", true);

ClientScript.RegisterHiddenField("lblDistance", "hii");

if i first press input button like this
<input type="button" value="Show" önclick="initialize();"/>
then after i press asp button
<asp:Button ID="btnFare" runat="server" Text="Calculate Fare" OnClientClick ="javascript:initialize();" onclick="btnFare_Click"/>

then its working.but if i directly try to call the function using asp.net button click then the value in the hidden field is not coming
it is showing error.still not able to figure out what to do with this.

thanks

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