Sending Friend Request Like Facebook in Social networking using asp.net csharp(c#)
- Friend Requests are a very important part of any social networking website.
- To Receive
the Friend requests we have created a user control.In this user control we have
shown the username ,photo along with accept and reject button.
- We are showing the
Friends Requests above the Friendlist so that the changes are refelected direclty
in the friendlist when the user accept or reject any friend request
FriendRequests.ascx Code:
- Open FriendRequests.ascx User Control Which You have created in Controls Folder.
- In the Source of FriendRequests.ascx user control you can add the following piece
of code given below.
- In FriendRequests user control we have created a tabular structure and in that tabular
structure we have dragged dropped one datalist and two link buttons for accept and
reject.
- Datalists Itemtemplate contains the user profile picture and also the username.
- As
always we are binding the image to profile picture using image handler.
- We have also
binded the RegisterId to Accept and Reject Button in the form of command argument
so that we can update the frienship status properly when the user clicks accept
or reject button.
- Finally we are adding this control to Friends.aspx page
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="FriendRequests.ascx.cs"
Inherits="Controls_FriendRequests" %>
<asp:DataList ID="dlFriendRequests" runat="server" Width="100%">
<ItemTemplate>
<table cellpadding="2" style="border: 1px ridge #CCCCCC; width: 100%;">
<tr>
<td class="style1" rowspan="2" align="center">
<asp:ImageButton ID="imgbtnYou" runat="server" BorderColor="#CCCCCC" BorderStyle="Ridge"
BorderWidth="1px" CommandArgument='<%#Bind("RegisterId") %>'
Height="84px" ImageUrl='<%# "ImageHandler.ashx?RegisterId="+ Eval("RegisterId") %>'
OnClick="imgbtnYou_Click" Width="84px" />
</td>
<td align="left">
<asp:Label ID="lblFriendName" runat="server" Font-Bold="False"
Font-Names="Franklin Gothic Medium"
Text='<%# Bind("Name") %>'></asp:Label>
</td>
</tr>
<tr>
<td>
<asp:Button ID="Accept" runat="server" ToolTip="Accept"
Text="Accept" OnClick="Accept_Click"
CommandArgument='<%# Bind("RegisterId") %>' />
<asp:Button ID="Reject" runat="server" ToolTip="Reject" Text="Reject" Width="59px"
CommandArgument='<%#Bind("RegisterId") %>' OnClick="Reject_Click" />
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
FriendRequests.ascx.cs C# Code:
- Now Open the Code behind file of Friends User Control. In FriendRequests.ascx.cs
file you can add the following piece of code given below.
- In code behind from page load method we are calling a function ShowFriendRequests().
- This
function will check for friends requests from database and if there is a friend
rquests it will bind it to the friendlist datalist.
- We have added a userid equals
currentprofileid so that we only see ours friend requests and not others friend
requests.
- To get the friend requests we have made a sub query between register table
and friends table and checking the friendship status as zero.while checking the
friend requests if the user clicks on the profile picture of the friendrequests
then we are diverting it to his her profile.
- We also have two Button Click Events
Accept and Reject.On Accept we are updating the friendship status with one and On
Reject we are updating the friendship status with two.
- To get the friendId we have
made use of Command Argument so that we can update the friendship status for particular
user only.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.UI.HtmlControls;
public partial class Controls_FriendRequests : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
ShowFriendRequests();
}
private void ShowFriendRequests()
{
if (Session["UserId"] == Session["CurrentProfileId"])
{
DataTable dt = new DataTable();
string query = "Select * FROM [Register] where RegisterId IN (SELECT MyId FROM Friends WHERE FriendId='" + Session["UserId"] + "' AND Status=0) ";
dt = Database.GetData(query);
if (dt.Rows.Count > 0)
{
dlFriendRequests.DataSource = dt;
dlFriendRequests.DataBind();
}
else
{
}
}
}
protected void imgbtnYou_Click(object sender, ImageClickEventArgs e)
{
Session["CurrentProfileId"] = (((ImageButton)sender).CommandArgument).ToString();
Response.Redirect("Main.aspx");
}
protected void Accept_Click(object sender, EventArgs e)
{
string SenderFriendId = (((Button)sender).CommandArgument).ToString();
string MyID = Session["UserId"].ToString();
string AcceptFriendQuery = "Update Friends set Status=1
where MyId='" + SenderFriendId + "' AND FriendId='" + MyID + "'";
Database.UpdateData(AcceptFriendQuery);
Response.Redirect("~/Friends.aspx");
}
protected void Reject_Click(object sender, EventArgs e)
{
string SenderFriendId = (((Button)sender).CommandArgument).ToString();
string MyID = Session["UserId"].ToString();
string RejectFriendQuery = "Update Friends set Status=2
where MyId='" + SenderFriendId + "' AND FriendId='" + MyID + "'";
Database.UpdateData(RejectFriendQuery);
Response.Redirect("~/Friends.aspx");
}
}
Friends.aspx Code:
- Once the FriendRequests.ascx user control is ready we have to add it to Search.aspx page
as shown below code snippet.
- Here we have done nothing but dragged dropped the user
control on the Friends.aspx page
<%@ Page Title="" Language="C#" MasterPageFile="~/ProfileMaster.master"
AutoEventWireup="true" CodeFile="Friends.aspx.cs" Inherits="Friends"
EnableEventValidation="false" %>
<%@ Register src="Controls/Friends.ascx" tagname="Friends" tagprefix="uc1" %>
<%@ Register src="Controls/FriendRequests.ascx" tagname="FriendRequests" tagprefix="uc2" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<table cellpadding="3" cellspacing="3" style="width: 100%">
<tr>
<td>
<uc2:FriendRequests ID="FriendRequests1" runat="server" />
</td>
</tr>
<tr>
<td align="center">
<asp:Label ID="Label1" runat="server" Text="Your Friends Your HeartBeats"
Font-Names="Franklin Gothic Medium"></asp:Label>
<br />
<hr />
</td>
</tr>
<tr>
<td>
<uc1:Friends ID="Friends1" runat="server" />
</td>
</tr>
<tr>
<td>
</td>
</tr>
</table>
</asp:Content>
Hi ,How to import facebook contacts to asp.net application
ReplyDeleteSend data base in SQL...easy to know...waiting....sir
ReplyDeleteanubhavyadavv100@gmail.com