-1
我試過一個隱藏的字段控件,但它不會將javascript值返回給c#後面的代碼。當我執行下面的代碼時,它總是返回0.如何在c#代碼後面獲取javascript值
如何獲得JavaScript到C#的距離值?
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
function GetDistance()
{
var origin = document.getElementById("orig").value,
destination = document.getElementById("dest").value,
service = new google.maps.DistanceMatrixService();
service.getDistanceMatrix(
{
origins: [origin],
destinations: [destination],
travelMode: google.maps.TravelMode.DRIVING,
avoidHighways: false,
avoidTolls: false
},
callback
);
function callback(response, status) {
alert('hello2');
var orig = document.getElementById("orig"),
dest = document.getElementById("dest"),
dist = document.getElementById("dist");
if (status == "OK") {
orig.value = response.originAddresses[0];
dest.value = response.destinationAddresses[0];
dist.value = response.rows[0].elements[0].distance.text;
document.getElementById('<%=hdnResultValue.ClientID%>').value = dist.value;
} else {
alert("Error: " + status);
}
}
}
</script>
<form id="frm1" runat="server" method="post">
<br>
Basic example for using the Distance Matrix.<br><br>
Origin: <input id="orig" type="text" style="width:35em"><br><br>
Destination: <input id="dest" type="text" style="width:35em"><br><br>
Distance: <input id="dist" type="text" style="width:35em">
<asp:Button ID="btnSubmit" runat="server" Text ="Submit Application" OnClientClick="GetDistance();" OnClick="btnSubmit_Click" />
<asp:Label ID="lblDistance" runat="server" Text=" " />
<asp:HiddenField ID="hdnResultValue" Value="0" runat="server" />
</form>
protected void btnSubmit_Click(object sender, EventArgs e)
{
lblDistance.Text = hdnResultValue.Value;
lblDistance.Text += "Calculated Successfully";
}
在後面的代碼中,你是否試圖獲取hdnResultValue的值? –