如何在c#代码中获取javascript值

编程入门 行业动态 更新时间:2024-10-26 12:34:02
本文介绍了如何在c#代码中获取javascript值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我尝试了隐藏的字段控件,但它没有返回java脚本值来查找c#中的代码。当我执行下面的代码时,它总是返回值0. 如何从java脚本到c#的距离值?

I tried hidden field control but it doesn't return java script value to find code behind in c#. When I execute below code, it returns value always 0. How to get distance value from java script to c#?

<script type="text/javascript" src="maps.google/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) { 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>

代码c#

code behind c#

protected void btnSubmit_Click(object sender, EventArgs e) { lblDistance.Text = hdnResultValue.Value; lblDistance.Text += "Calculated Successfully"; }

推荐答案

问题是您在 AJAX调用之前提交表单完成。您需要延迟表单提交,直到调用回调函数。 这样的事情可能有效: The problem is that you're submitting the form before the AJAX call completes. You need to delay the form submission until the callback function has been called. Something like this might work: var submitFromCode = false; function GetDistance(){ // If the button was triggered by the "callback" function, submit the form: if (submitFromCode) { return true; } 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 }, function(response, status) { 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; // Submit the form: submitFromCode = true; document.getElementById('<%=btnSubmit.ClientID%>').click(); } else { alert("Error: " + status); } }); // Prevent the form from being submitted until the "callback" function is called: return false; }

尝试直接从表单访问它: try accessing it from the form directly: <input type="hidden" id="hdnResultValue" name="hdnResultValue" value="" />

String test = Request.Form["hdnResultValue"];

更新:测试版本: Page1.aspx:

UPDATE: Tested Version: Page1.aspx:

<html xmlns="www.w3/1999/xhtml"> <head runat="server"> <title></title> <script type="text/javascript" > function btnClick() { document.getElementById("hdnHidden").value = "test"; } </script> </head> <body> <form id="form1" runat="server"> <input type="hidden" id="hdnHidden" name="myHidden" /> <button type="button" id="btnButton" name="myButton" onclick="btnClick();" >btnClick</button> <button type="submit">submit</button> </form> </body> </html>

Page1.aspx.cs:

Page1.aspx.cs:

using System; namespace WebTests { public partial class Page1 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { string test = Request.Form["myHidden"]; } protected override void OnLoad(EventArgs e) { base.OnLoad(e); string test = Request.Form["myHidden"]; } } }

页面有两个按钮。 btnClick运行更改隐藏字段值的javascript。 submit提交包含隐藏字段的表单。 在两种页面加载方法中,字符串测试的值为test 看看mu代码,将它与你的代码进行比较并尝试查看不同的内容,即提交的表单中是否为hdnResultValue?

The page has two buttons. "btnClick" runs the javascript that changes the hidden fields value. "submit" submits the form containing the hidden field. In both page load methods the value of "string test" is "test" Take a look at mu code, compare it to yours and try to see whats different, i.e. is hdnResultValue within the form being submitted?

更多推荐

如何在c#代码中获取javascript值

本文发布于:2023-11-16 16:18:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1605870.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:代码   如何在   javascript

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!