javascript - 如何在页面加载时将内部 html 的值获取到 C# 代码隐藏中

标签 javascript c# asp.net code-behind

嗨,我在java脚本中有这段代码,我需要后面代码中的ip值

 window.onload = function () {
        var script = document.createElement("script");
        script.type = "text/javascript";
        script.src = "http://www.telize.com/jsonip?callback=DisplayIP";
        document.getElementsByTagName("head")[0].appendChild(script);
    };
    function DisplayIP(response) {
        **document.getElementById("ipaddress").innerHTML = "Your IP Address is " + response.ip;**

    }

最佳答案

您可以在 JavaScript 中设置隐藏字段。在 ASP 代码中创建一个字段:

<input type="hidden" id="ipHidden" runat="server" />

在 JavaScript 中添加以下内容:

    function DisplayIP(response) {
        document.getElementById("ipaddress").innerHTML = "Your IP Address is " + response.ip;
        document.getElementById("ipHidden").value = response.ip;
    }

在 C# 中:

string clientIP = ipHidden.value;

关于javascript - 如何在页面加载时将内部 html 的值获取到 C# 代码隐藏中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27142546/

相关文章:

c# - 测试链接的最有效方法

asp.net - Web.Config 的 System.Web 部分中的 AllowLocation ="true"有什么作用?

javascript - 以编程方式触发文本输入上的 keyup 事件

javascript - 用不重复的随机数填充数组

c# - 使用 IronPython 在 C# 中运行特定的 Python 函数

c# - 如何在单击的菜单项上将事件类附加到现有类?

asp.net - 如何在 ABC 中打断表格pdf

javascript - Node (Gulp)process.stdout.write 到文件

javascript - Internet Explorer 8 事件因透明 parent 而失败

c# - 操纵杆 + c# : Analog button