ARTICLE

How to use Facebook Share functionality in ASP.NET

Posted by Raj Kumar Articles | ASP.NET Programming October 18, 2010
This article demonstrates how to use Facebook share button functionality with master page or without master page in asp.net.
Reader Level:

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.

Login to add your contents and source code to this article
Article Extensions
Contents added by Ehab Mohmd on Feb 28, 2013
??? ???? ?????? ??????
Contents added by nguyen van minh on Feb 27, 2013
sg dfhdfhfg hfgh fghgfhfgh gfh
post comment
     

Hi, I am getting the below message. "The message could not be posted to this Wall."

Posted by Reddy E Dec 05, 2012

Hi Raj! this post was really helpfull for me as I am quite a new programmer. this worked really fine but i face some problem as it works fine in IE8 on the localhost but when I put it on the real page it works fine withe firefox and chrome but didn't work with IE8 . I have also tried to write the xml validation in the html tag but it also didn't worked. in IE8 it dosent show the fb share icon but just an ordinary link text and it dosent work. my actual requirement is that I have an advertisement website where people can upload there products so I want that if a person assa a product he can see a lfb share link on the page so if he wants to post it on his fb wall that he has uploaded a product to my website. And I also want the thumbnail of the product he uploaded on his fb wall. thanks in advane

Posted by Mansoor Ahmad May 10, 2011

Thank you for this article

I'm still having a problem because I'm displaying a link to a newssite.   I would like the content of the news to appear on facebook instead of the link itself like you do

hope you can help me

regards BN




Posted by Bjork Bragadottir Nov 18, 2010

Really it is helpful

Posted by Rohatash Kumar Oct 20, 2010

Keep them coming :)

Posted by Mahesh Chand Oct 18, 2010
COMMENT USING
PREMIUM SPONSORS
Over-C is a holistic consortium of communications and technology specialists. We build, deploy and market both business as well as consumer products and solutions.
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.