Hi micah,
I have created sample code which fullfill your requirement.
HTML
<form id="form1" runat="server">
<div>
<asp:DataList ID="DataList1" runat="server" RepeatLayout="Table" RepeatColumns="1">
<ItemTemplate>
<table>
<tr>
<td>
<asp:HiddenField ID="HiddenField2" Value='<%#Eval("likeId") %>' runat="server"></asp:HiddenField>
<asp:HiddenField ID="HiddenField1" Value='<%#Eval("Id") %>' runat="server"></asp:HiddenField>
<asp:Label ID="lblMessage" CssClass="Message" Text='<%#Eval("ContentPost") %>' runat="server"></asp:Label>
<asp:Label ID="lblCount" CssClass="Count" Text='<%#Eval("TotalCount") %>' runat="server" />
<asp:LinkButton ID="btnLike" OnClick="Update" Text='<%#Eval("LikeCount").ToString() == "0" ? "Like":"UnLike" %>'
runat="server" />
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
</div>
<div>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
var result = 1;
$('[id*=btnLike]').click(function () {
var posttableID = $(this).parent().parent().find("[id*=HiddenField1]").val();
var likeId = $(this).parent().parent().find("[id*=HiddenField2]").val();
if (likeId == '') {
likeId = 0;
}
var buttonText = $(this).html();
$.ajax({
type: "POST",
url: "Default.aspx/InsertLike",
data: '{result: "' + result + '", posttableID: "' + posttableID + '",buttonText:"' + buttonText + '",LikeId:"' + likeId + '"}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
}
});
});
});
</script>
</div>
</form>
C#
private string constr = ConfigurationManager.ConnectionStrings["Constring"].ToString();
protected void Page_Load(object sender, EventArgs e)
{
this.GetData();
}
private void GetData()
{
SqlConnection con = new SqlConnection(constr);
SqlCommand cmd = new SqlCommand("SELECT UserPost.Id,UserPost.ContentPost,likes.Id as likeId,CASE WHEN Likes.LikeCount IS NULL THEN '0' END AS LikeCount,TotalCount FROM UserPost LEFT JOIN Likes ON UserPost.Id = Likes.posttableId", con);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
DataList1.DataSource = dt;
DataList1.DataBind();
}
[WebMethod]
public static string InsertLike(int result, string posttableID, string buttonText, int LikeId)
{
int FrndUserId = 2;
string constr = ConfigurationManager.ConnectionStrings["Constring"].ToString();
SqlConnection con = new SqlConnection(constr);
SqlCommand cmd = new SqlCommand("InsertLikes1", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@LikeCount", result);
cmd.Parameters.AddWithValue("@posttableID", posttableID);
cmd.Parameters.AddWithValue("@FriendUserName", FrndUserId);
cmd.Parameters.AddWithValue("@UserName", 1);
cmd.Parameters.AddWithValue("@Id", LikeId);
cmd.Parameters.AddWithValue("@Action", buttonText.ToLower());
con.Open();
object action = cmd.ExecuteScalar();
con.Close();
return action.ToString();
}
protected void Update(object sender, EventArgs e)
{
this.GetData();
}
SQL
CREATE PROCEDURE [dbo].[InsertLikes1](
@posttableID int,
@FriendUserName nvarchar(200),
@UserName nvarchar(200),
@Action VARCHAR(10),
@LikeCount INT,
@ID INT
)
AS
BEGIN
if(@Action = 'like')
BeGIN
IF EXISTS(SELECT * FROM Likes WHERE Id = @ID )
BEGIN
UPDATE Likes SET LikeCount = 1 WHERE Id = @ID
UPDATE UserPost SET [TotalCount] = [TotalCount] + @LikeCount WHERE [Id] = @posttableID
END
ELSE
BEGIN
INSERT INTO Likes VALUES(@FriendUserName,@UserName,@LikeCount,@posttableID)
UPDATE UserPost SET [TotalCount] = [TotalCount] + @LikeCount WHERE [Id] = @posttableID--AND [UserName] =@UserName
END
END
IF(@Action = 'unlike')
BEGIN
UPDATE Likes SET LikeCount = NULL WHERE Id = @ID
UPDATE UserPost SET [TotalCount] = [TotalCount] - @LikeCount WHERE [Id] = @posttableID
END
SELECT @Action
END
Screenshot