简体   繁体   English

如何在页面加载时将内部html的值转换为c#代码

[英]how to get the value of inner html into c# codebehind on page load

Hi i have this code in java script i need the value of ip in the 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;**

    }

You can set hidden field in your JavaScript. 您可以在JavaScript中设置隐藏字段。 Create a field in your ASP code: 在您的ASP代码中创建一个字段:

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

In your JavaScript add this: 在您的JavaScript中添加以下内容:

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

In C#: 在C#中:

string clientIP = ipHidden.value;

you cannot get IP field on server side because the your java script will execute after your code behind finishes execution. 您无法在服务器端获取IP字段,因为您的Java脚本将在您的代码完成执行后执行。 you will be able to get it only when page finishes its rendering 您只有在页面完成渲染后才能获取它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM