Search code examples
c#javascriptfacebookdialogcode-behind

How to run Facebook Social Plugin Javascript From CodeBehind C#?


I have a website where i want my visitors to invite their friends. I'm using the Requests Dialog for this. But now i want to add a dynamic text message to this invite.

The dynamic text is generated in an c# code behind file. But the Request Dialog is triggered with javascript.

Here is the Request Dialog part on my aspx page:

  <div id="fb-root"></div>
  <script src="http://connect.facebook.net/nl_NL/all.js">
  </script>
  <script>
      FB.init({
          appId: '[MY-APP-ID]', cookie: true,
          status: true, xfbml: true
      });

      function inviteFriends() {
          FB.ui({ method: 'apprequests', message: '[THE DYNAMIC MESSAGE I WANT TO SEND]', data: 'tracking information for the user' });
      }


  </script>

Is it possible to call this javascript form codebehind code and pass the message parameter too?

Thanks in advance!


Solution

  • You can use <%= ... %> to expose your code to page (assuming you have message in requestsDialogMessage variable):

    FB.ui({
      method: 'apprequests',
      message: '<%= requestsDialogMessage %>',
      data: 'tracking information for the user'
    });
    

    Other way may be exposing the message to some JavaScript variable and using it within FB.ui call:

    <script>
      var exposedData = {
        requestsDialogMessage: '<%= requestsDialogMessage %>'
      };
    </script>
    

    And later in your JS code:

    FB.ui({
      method: 'apprequests',
      message: exposedData.requestsDialogMessage,
      data: 'tracking information for the user'
    });