0

This is my 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;
        }
    });
}

Here I want to assign var user_email to asp label control. How should I do?

feeeper
  • 2,865
  • 4
  • 28
  • 42
Prady
  • 45
  • 9

1 Answers1

0

You have to use ClientID property to get client-side element:

<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>

And MyService.asmx.cs:

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

    [WebMethod]
    public void SendUserEmail(userEmail userEmail)
    {
       // do some stuff with userEmail
    }
}
feeeper
  • 2,865
  • 4
  • 28
  • 42
  • feeeper... Thank you... I was not getting that syntax. I'll try this one. – Prady Sep 08 '16 at 06:23
  • but i did not get that how it is going to assign 'var user_email' value to that label. Will you please elaborate? – Prady Sep 08 '16 at 06:26
  • @Prady Do you mean something like `document.getElementById('<%=lblfbid.ClientID %>').innerHTML = user_email;` ? – feeeper Sep 08 '16 at 06:29
  • @Prady Sure, you can – feeeper Sep 08 '16 at 06:33
  • Why I asked this... because I want to assign that user_email variable's value to lblfbid. – Prady Sep 08 '16 at 06:35
  • I am not getting any error. The problem is I am logging in to my application with facebook. And I want to display that name or email on page next to login page. but it is redirecting on login page only. – Prady Sep 08 '16 at 07:26
  • hence i am trying to assign that variable to asp control so that I can access it on my .cs page – Prady Sep 08 '16 at 07:27
  • @Prady It's impossible do that that way. You have to send your `user_email` to the server via AJAX (create some asmx service and call it) or call `__doPostback` (AJAX imho more suitable) – feeeper Sep 08 '16 at 08:50
  • ohhhh... mmm... ok... i'll try – Prady Sep 08 '16 at 09:08
  • can I take that value in session? and can i use that session on my cs page? – Prady Sep 08 '16 at 09:12
  • @Prady If you save `user_email` into the session then you can get it from session. – feeeper Sep 08 '16 at 09:14
  • from aspx page to cs page? Is it possible? – Prady Sep 08 '16 at 09:18
  • @Prady Nope. You can save `user_email` to the Session in the *.asmx and then use `user_email` from there. – feeeper Sep 08 '16 at 09:28