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

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

本文关键字:变量值 asp 控件 标签 有一个 JavaScript 变量 我在      更新时间:2023-09-26

这是我的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
    }
}