Javascript in Asp.net,C#

本文关键字:net Asp in Javascript | 更新日期: 2023-09-27 18:29:02

我在asp.net中使用javascript。我想在codebehind中使用javascript的数据。我该怎么做?

function init()
{
...
...
trigger: function (e) {
    var lonlat = map.getLonLatFromPixel(e.xy);
    N = lonlat.lat;
    E = lonlat.lon;
    alert("You are near hear: " + N + " N, " + E + " E");
}

如何将N和E变量发送到代码隐藏。(所以后面的代码可用)

Javascript in Asp.net,C#

类似这样的东西:

这里Hiddenfield1 and Hiddenfield2是两个html隐藏字段

JS-

document.getElementById("Hiddenfield1").value=N;
document.getElementById("Hiddenfield2").value=E;

在C#中

Response.Write(Hiddenfield1.Value);
Response.Write(Hiddenfield2.Value);

试试这样的东西:-

<script type="text/javascript">
function init()
{ bla bla bla
trigger: function (e) {
var lonlat = map.getLonLatFromPixel(e.xy);
N = lonlat.lat;
E = lonlat.lon;
document.getElementById("Hidden1").value=N;
document.getElementById("Hidden2").value=E;
alert("You are near hear: " + N + " N, " + E + " E");
}

</script>
<body>
    <form id="form1" runat="server">
    <div>
        <input id="Hidden1" type="hidden" runat="server" />
        <input id="Hidden2" type="hidden" runat="server" />
        <asp:Button ID="Button1" runat="server" OnClientClick="init()"  Text="Button"
            onclick="Button1_Click" />
    </div>
    </form>
</body>

在代码后面:-

protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Write(Hidden1.Value);
        Response.Write(Hidden2.Value);
    }

最简单的方法是向页面添加两个控件。

<asp:HiddenField runat="server" ID="lat" />
<asp:HiddenField runat="server" ID="lon" />

在触发功能中设置字段值:

document.getElementById('lat').value = lonlat.lat;
document.getElementById('lon').value = lonlat.lon;

在服务器端代码中,采用控件的Text属性。