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

时间:2016-09-08 05:59:33

标签: javascript asp.net label variable-assignment var

这是我的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标签控件。我该怎么办?

1 个答案:

答案 0 :(得分:0)

您必须使用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
    }
}