How to use Facebook Share functionality in ASP.NET


This article demonstrates how to use Facebook share button functionality with master page or without master page in asp.net.

The use of facebook share button is when you click on share button which opens a Facebook share dialog. If user has facebook account then they can then share the web page on their Facebook "wall" with their friends on Facebook, adding comments. Their friends will have the opportunity to share with their friends, and so on.

Let's start without master page first.

First of all make a new asp.net website and put this code on page.

First of all put this script inside of head tag.

<head runat="server">
    <title>Facebook share sampel</title>
    <script src="http://static.ak.fbcdn.net/connect.php/js/FB.Share" type="text/javascript"></script>
<script src="http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php"
           type="text/javascript">
</script>
</head>

And now add a html link button on page.

<form id="form1" runat="server">
    <div>
    <a name="sharebutton" type="button" href="http://www.facebook.com/sharer.php">Share</a>  
    </div>
</form>

Now run the application.

1.JPG
 
Image 1.

When you click on Share button..one dialog window will open.

2.JPG
 
Image 2.

You can send as a message instead to friends using to click a link button below of dialog window.

3.JPG
 
Image 3.

Now put you message in message textbox and click Share or Send Message buton, you will see a message window.

4.JPG
 
Image 4.

To see you message login on facebook  and click on Profile button from top of header.

5.JPG 

Image 5.

Now let's work master page.

Drag and Drop a label inside of content place holder.

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server">
<asp:Label ID="lblShare" runat="server" Text=""></asp:Label>
</asp:Content>>

Code Behind.

using System.Web.UI.HtmlControls;
protected void Page_Load(object sender, EventArgs e)
{      
    lblShare.Text = "<a name=\"fb_share\" type=\"button\"></a>" +
                    "<script " +
                    "src=\"http://static.ak.fbcdn.net/connect.php/js/FB.Share\" " +
                    "type=\"text/javascript\"></script>";
    HtmlMeta tag = new HtmlMeta();
    tag.Name = "title";
    tag.Content = "This is the page title";
    Page.Header.Controls.Add(tag);
    HtmlMeta tag1 = new HtmlMeta();
    tag.Name = "description";
    tag.Content = "This is a page description.";
    Page.Header.Controls.Add(tag1);           
}

And finally run application.

Up Next
    Ebook Download
    View all
    Learn
    View all