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

问题描述:

我需要在c#中获取代码背后的javascript值。我知道我可以使用隐藏字段,但页面上没有用于回发的服务器控件。请告诉我如何在代码中获取代码。

I need to get javascript values on code behind in c#.I know i can use hidden field but there is no server control on page for postback.Please tell me how can get vales in code behind.

这是我的代码:

<html>
<head>
<title>Facebook Get Logged in User Details UserName,Email,Profile Image</title>
    <script src="jquery-1.6.2.min.js" type="text/javascript"></script>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
<script>
    // Load the SDK Asynchronously



    (function (d) {
        var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
        if (d.getElementById(id)) { return; }
        js = d.createElement('script'); js.id = id; js.async = true;
        js.src = "//connect.facebook.net/en_US/all.js";
        ref.parentNode.insertBefore(js, ref);
    } (document));

    // Init the SDK upon load
    window.fbAsyncInit = function () {
        FB.init({
            appId: 'APPID', // App ID
            channelUrl: '//' + window.location.hostname + '/channel', // Path to your Channel File
            status: true, // check login status
            cookie: true, // enable cookies to allow the server to access the session
            xfbml: true  // parse XFBML
        });

        // listen for and handle auth.statusChange events
        FB.Event.subscribe('auth.statusChange', function (response) {
            if (response.authResponse) {
                // user has auth'd your app and is logged into Facebook
                var uid = "http://graph.facebook.com/" + response.authResponse.userID + "/picture";
                FB.api('/me', function (me) {
                    document.getElementById('auth-displayname').innerHTML = me.name;
                    document.getElementById('myJSString').value = me.name;

                    alert(document.getElementById('myJSString').value);

                    document.getElementById('Email').innerHTML = me.email;
                    document.getElementById('profileImg').src = uid;

                    //  document.getElementById('ctl00_CPHDefault_tcTPS_TPProd_ctl01_tcProduction_TPNewT‌​itlesStatus_ChangedRowsIndicesHiddenField').value = uid;
                   // alert('yyy');

                })
                document.getElementById('auth-loggedout').style.display = 'none';
                document.getElementById('auth-loggedin').style.display = 'block';
            } else {
                // user has not auth'd your app, or is not logged into Facebook
                document.getElementById('auth-loggedout').style.display = 'block';
                document.getElementById('auth-loggedin').style.display = 'none';
            }
        });
        $("#auth-logoutlink").click(function () { FB.logout(function () { window.location.reload(); }); });
    }





</script>
<h1>
Facebook Login Authentication Example</h1>
<div id="auth-status">
<div id="auth-loggedout">
<div id="Result"  class="fb-login-button" autologoutlink="true" scope="email,user_checkins">Login</div>
</div>
<div id="auth-loggedin" style="display: none">
Name: <b><span id="auth-displayname"></span></b>(<a href="#" id="auth-logoutlink">logout</a>)<br />
Email: <b><span id="Email"></span></b><br />
Profile Image: <img id="profileImg" />

<form runat="server">
<asp:HiddenField runat="server" id="myJSString" />

</form>
</div>
</div>
</body>
</html>

你可以看到没有服务器控件,所以我怎么能在后面的代码中获取NAME,UID变量。

You can see there is no server control so how i can get NAME,UID variables in code behind.

谢谢

我会研究使用ASP.NET AJAX页面方法,因为它们允许生活在 .aspx 页面中的脚本可调用的独立Web服务,如下所示:

I would investigate the use of ASP.NET AJAX Page Methods, because they allow for script callable stand-alone web services that live in an .aspx page, like this:

代码隐藏文件中的页面方法(出于讨论的目的将其命名为default.aspx):

Page Method in your code-behind file (call it default.aspx for discussion's sake):

[WebMethod]
public static string SaveData(string name, string uid)
{
    // Logic here to do what you want with name and uid values (i.e. save to database, call another service, etc.)
}

jQuery调用default.aspx的SaveData方法:

jQuery call to default.aspx's SaveData method:

$.ajax({
    type: "POST",
    url: "default.aspx/SaveData",
    data: "{'name':'John', 'uid':'ABC123'}",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(msg) {
        // Do something interesting here.
    }
});

注意:ASP.NET AJAX页面方法自动将其响应编码为 JSON 所以你不会在代码隐藏或任何序列化逻辑中看到任何 JSON 序列化。

Notes: ASP.NET AJAX Page Methods automatically encode their response to JSON so you will not see any JSON serialization in the code-behind or any serialization logic at all.

有关ASP.NET AJAX页面方法的更多信息,请查看使用jQuery直接调用ASP.NET AJAX页面方法

For more information about ASP.NET AJAX Page Methods check out Using jQuery to directly call ASP.NET AJAX page methods