Wall and Latest Updates User Control for Social Networking website Using Asp.net,Csharp
- Wall just like facebook is an very impoartant part of social networking website
where user can express their feelings and emotions.
- It is a place where our friends
can also write what they feel about us.We have also taken care that we can only
see our friends wall
LatestUpdates.ascx Code:
- Open LatestUpdates.ascx User Control Which You have created in Controls Folder.
- In the Source of LatestUpdates.ascx user control you can add the following piece
of code given below.
- In this user control first we have a Add as friend Button.
- If you are on your friends
profile then this add as friend button is not visible.
- If the Friend request is pending
it will show as the friend request pending status.
- Next is inside an ajax update
panel which we have used for partial staus update.It will also show the progress
bar while updating the wall status.
- We have a text box where we can type our status and that status will be updated
on our wall.
- Our Status updates are binded to datalist with the profile picture status,posted
by and post date.we have associated AsyncPostBackTrigger with the button post so
that there is no page refresh when we update our status on heartbeat
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="LatestUpdates.ascx.cs"
Inherits="Controls_LatestUpdates" %>
<style type="text/css">
.styleLatestUpdates
{
width: 400px;
}
.stylePost
{
width: 98%;
height: 90px;
}
.styleComment
{
width: 103px;
}
.stylePostPic
{
width: 110px;
vertical-align: top;
}
</style>
<table style="width: 550px" align="center">
<tr>
<td align="left" style="padding-left: 10px;">
<asp:Label ID="lblName" runat="server" Font-Bold="True" Font-Names="Georgia" Font-Size="Large"></asp:Label>
</td>
<td align="right">
<asp:LinkButton ID="btnAddAsFriend" runat="server" Text="Add As Friend" Font-Bold="True"
Font-Italic="True" OnClick="btnAddAsFriend_Click"></asp:LinkButton>
<br />
</td>
</tr>
</table>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<table align="center" cellpadding="0" cellspacing="0" class="styleLatestUpdates">
<tr>
<td>
<table style="width: 550px">
<tr>
<td align="left">
<panel runat="server" id="pnlStatus">
<asp:TextBox ID="txtWhatsOnYourHeart" runat="server" Height="59px" TextMode="MultiLine"
Width="543px"></asp:TextBox>
<br />
<asp:Button ID="btnPost" runat="server" BackColor="#CCCCCC" BorderColor="#CCCCCC"
BorderStyle="Ridge" BorderWidth="1px" Font-Bold="True" ForeColor="#333333" OnClick="btnPost_Click"
Text="Post" Width="52px" />
<asp:UpdateProgress ID="ProgressBar" runat="server">
<ProgressTemplate>
<asp:Image ID="Image1" runat="server" ImageUrl="~/images/loading.gif" />
</ProgressTemplate>
</asp:UpdateProgress>
</panel>
</td>
</tr>
</table>
<hr />
</td>
</tr>
<tr>
<td>
<asp:DataList ID="dlPosts" runat="server" Width="550px">
<ItemTemplate>
<div>
<table cellpadding="0" cellspacing="0" class="stylePost">
<tr>
<td class="stylePostPic" rowspan="2" align="center">
<asp:ImageButton ID="YourPic" runat="server" BorderColor="Black" BorderStyle="Ridge"
BorderWidth="1px" Height="60px" ImageUrl='<%# "ImageHandler.ashx?RegisterId="+ Eval("RegisterId") %>'
Width="60px" OnClick="YourPic_Click" CommandArgument='<%#Bind("RegisterId") %>' />
</td>
<td valign="top" align="left">
<asp:Label runat="server" ID="lblName" ForeColor="Blue" Font-Bold="True" Font-Italic="True">
<%# DataBinder.Eval(Container.DataItem, "Name")%> Posted:</asp:Label>
<div style="width: 300px">
<%# DataBinder.Eval(Container.DataItem, "Post")%>
</div>
</td>
</tr>
<tr>
<td align="right" valign="bottom">
<asp:Label runat="server" ID="lblPostedOn" ForeColor="Black" Font-Italic="True" Font-Size="Small">
Posted On: <%# DataBinder.Eval(Container.DataItem, "PostDate")%>
</asp:Label>
</td>
</tr>
</table>
<br />
<hr />
</div>
</ItemTemplate>
</asp:DataList>
</td>
</tr>
</table>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnPost" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
LatestUpdates.ascx.cs C# Code:
Now Open the Code behind file of Friends User Control. In LatestUpdates.ascx.cs
file you can add the following piece of code given below.
From the Page Load method we are calling two methods LatestUpdates() and CheckFriendshipStatus().LatestUpdates
function is getting the latest updates from database and check friendship status
is checking the status of friendship so that we can hide and show the wall and add
as friend button.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
public partial class Controls_LatestUpdates : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
LatestUpdates();
CheckFriendshipStatus();
}
private void LatestUpdates()
{
if (Session["Name"]!=null)
{
lblName.Text = Session["Name"].ToString();
}
DataTable dt = new DataTable();
string query = "SELECT u.RegisterId,u.Name,s.FromId,s.ToId,s.Post,s.PostId,s.PostDate FROM [Register] as u, Posts as s WHERE u.RegisterId=s.FromId AND s.ToId='" + Session["CurrentProfileId"] + "' order by s.PostId desc";
dt = Database.GetData(query);
if (dt.Rows.Count > 0)
{
dlPosts.Visible = true;
dlPosts.DataSource = dt;
dlPosts.DataBind();
}
else
{
}
}
private void CheckFriendshipStatus()
{
if (Object.Equals(Session["UserId"].ToString(), Session["CurrentProfileId"].ToString()))
{
btnAddAsFriend.Visible = false;
}
else
{
DataTable dt1 = new DataTable();
string chkfriendRequest = "SELECT * FROM Friends WHERE (MyId='" + Session["UserId"].ToString() + "' and FriendId='" + Session["CurrentProfileId"].ToString() + "') OR (MyId='" + Session["CurrentProfileId"].ToString() + "' and FriendId='" + Session["UserId"].ToString() + "')";
dt1 = Database.GetData(chkfriendRequest); ;
if (dt1.Rows.Count > 0)
{
if (dt1.Rows[0]["Status"].ToString() == "1")
{
//lblError.Text = "Already in friend list";
btnAddAsFriend.Visible = false;
}
if (dt1.Rows[0]["Status"].ToString() == "0")
{
//lblError.Text = "Friend Request Pending";
btnAddAsFriend.Visible = true;
btnAddAsFriend.Text = "Friend Request Pending";
btnAddAsFriend.Enabled = false;
pnlStatus.Visible = false;
dlPosts.Visible = false;
}
if (dt1.Rows[0]["Status"].ToString() == "2")
{
//lblError.Text = "Friend Request deny";
pnlStatus.Visible = false;
dlPosts.Visible = false;
}
}
else
{
pnlStatus.Visible = false;
dlPosts.Visible = false;
}
}
}
protected void btnPost_Click(object sender, EventArgs e)
{
string PostDate = DateTime.Now.ToLongDateString();
string InsertPostquery = "Insert into Posts (Post,FromId,ToId,PostDate)values('" + txtWhatsOnYourHeart.Text + "','" + Convert.ToInt32(Session["UserId"]) + "','" + Convert.ToInt32(Session["CurrentProfileId"]) + "','" + PostDate + "')";
Database.InsertData(InsertPostquery);
LatestUpdates();
txtWhatsOnYourHeart.Text = string.Empty;
}
protected void btnAddAsFriend_Click(object sender, EventArgs e)
{
if (!object.Equals(Session["UserId"], null))
{
if (Object.Equals(Session["UserId"], Session["CurrentProfileId"]))
{
btnAddAsFriend.Visible = false;
}
else
{
DataTable dt1 = new DataTable();
string chkfriendRequest = "SELECT * FROM Friends WHERE (MyId='" + Session["UserId"].ToString() + "' and FriendId='" + Session["CurrentProfileId"].ToString() + "') OR (MyId='" + Session["CurrentProfileId"].ToString() + "' and FriendId='" + Session["UserId"].ToString() + "')";
dt1 = Database.GetData(chkfriendRequest); ;
if (dt1.Rows.Count > 0)
{
if (dt1.Rows[0]["Status"].ToString() == "1")
{
//lblError.Text = "Already in friend list";
btnAddAsFriend.Visible = false;
}
if (dt1.Rows[0]["Status"].ToString() == "0")
{
//lblError.Text = "Friend Request Pending";
btnAddAsFriend.Visible = true;
btnAddAsFriend.Text = "Friend Request Pending";
btnAddAsFriend.Enabled = false;
}
if (dt1.Rows[0]["Status"].ToString() == "2")
{
//lblError.Text = "Friend Request deny";
}
}
else
{
string friendRequest = "Insert INTO Friends (MyId,FriendId,Status) VALUES('" + Session["UserId"].ToString() + "','" + Session["CurrentProfileId"].ToString() + "','" + 0 + "')";
Database.InsertData(friendRequest);
btnAddAsFriend.Text = "Friend Request Sent.";
}
}
}
else
{
Response.Redirect("~/Login.aspx");
}
}
protected void YourPic_Click(object sender, ImageClickEventArgs e)
{
Session["CurrentProfileId"] = (((ImageButton)sender).CommandArgument).ToString();
Response.Redirect("Main.aspx");
}
}