繁体   English   中英

我在JavaScript中有一个变量,我想将该变量值分配给asp标签控件

[英]I have a variable in JavaScript and I want to assign that variable value to asp label control

这是我的JavaScript

if (response.authResponse) {
    // user has auth'd your app and is logged into Facebook        
    var user_email = response.authResponse.id;

    FB.api('/me', function (me) {
        if (me.name) {
            document.getElementById('auth-displayname').innerHTML = me.name;
        }
    });
}

在这里,我想将var user_email分配给asp标签控件。 我应该怎么做?

您必须使用ClientID属性来获取客户端元素:

<asp:Label ID="lblfbid" runat="server" ></asp:Label>
...
<script>
...
if (response.authResponse) {
    // user has auth'd your app and is logged into Facebook        
    var user_email = response.authResponse.id;

    FB.api('/me', function (me) {
        if (me.name) {
            // Just for client-side
            document.getElementById('<%=lblfbid.ClientID %>').innerHTML = user_email;
            // Send user_email on server-side ($.post is from jQuery) for some reason: 
            $.post(
              '/MyService.asmx/SendUserEmail', 
              { userEmail: user_email }, 
              function() { /* do something on success */ });
        }
    });
}
...
</script>

MyService.asmx.cs

public class MyService : System.Web.Services.WebService
{

    [WebMethod]
    public void SendUserEmail(userEmail userEmail)
    {
       // do some stuff with userEmail
    }
}

暂无
暂无

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

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