2

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

这是我的代码:

<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 变量。

谢谢

4

3 回答 3

7

您可以使用隐藏字段服务器控件在 javascript 中为其分配所需的值并在服务器端对其进行评估。如果您不想回帖,则可以使用jQuery ajax发送值。

html

<asp:hiddenfield id="ValueHiddenField" runat="server"/>

Javascript

document.getElementById('ValueHiddenField').value = "yourValue";

背后的代码

string yourValue = ValueHiddenField.Value;

使用jQuery ajax 和 web 方法将值发送到代码后面,你可以在这里找到很好的教程。

$.ajax({
  type: "POST",
  url: "PageName.aspx/MethodName",
  data: {'yourParam': '123'},
  contentType: "application/json; charset=utf-8",
  dataType: "json",
  success: function(msg) {
    // Do something interesting here.
  }
});

背后的代码

[WebMethod]
public static void YourMethod(string yourParam)
{
   //your code goes here
}
于 2013-07-17T12:58:43.477 回答
1

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

代码隐藏文件中的页面方法(为了讨论,将其称为 default.aspx):

[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 方法:

$.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在代码隐藏中看到任何序列化或任何序列化逻辑。

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

于 2013-07-17T13:15:12.380 回答
0

您可以使用以下方法:

<script language="javascript" type="text/javascript">
    function returnString() {
        var val = 'sampleValue';
        return val;
    }
</script>

获取上述函数返回值的C#代码:

ClientScript.RegisterClientScriptBlock(this.GetType(), "alertScript", "<script language="javascript">var a=returnString();alert(a);</script>");

或者就像 Adil 所说的,可以使用隐藏字段并赋值:

<asp:HiddenField ID="hField" Value="0" runat="server" />
        <asp:Button ID="Button1" runat="server"  OnClientClick="returnString();"
            Text="Button" onclick="Button1_Click" />

赋值脚本:

<script language="javascript" type="text/javascript">
       function returnString() {
           debugger;
           document.getElementById("hField").value = "sampleValue";
       }
   </script>
于 2013-07-17T13:04:23.167 回答