【问题标题】:I have a variable in JavaScript and I want to assign that variable value to asp label control我在 JavaScript 中有一个变量,我想将该变量值分配给 asp 标签控件
【发布时间】:2016-09-08 05:59:33
【问题描述】:

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

【问题讨论】:

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


【解决方案1】:

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

【讨论】:

  • feeeper...谢谢...我没有得到那种语法。我会试试这个。
  • 但我不知道它将如何将 'var user_email' 值分配给该标签。请详细说明一下?
  • @Prady 你的意思是像document.getElementById('&lt;%=lblfbid.ClientID %&gt;').innerHTML = user_email; 吗?
  • @Prady 当然可以
  • 为什么我问这个...因为我想将该 user_email 变量的值分配给 lblfbid。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-04
  • 1970-01-01
  • 2020-07-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-31
相关资源
最近更新 更多