I am implementing like and dislike on my asp.net page. Here is the aspx
page
<asp:UpdatePanel ID="VotePanel" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="false">
<ContentTemplate>
<div class="row">
<!-- left column -->
<div class="col-lg-12 col-sm-12 col-xs-12">
<div class="text-center">
<h1>
<asp:Label ID="LikeLabel" runat="server" Text="Label"></asp:Label>
<br />
<asp:Label ID="DislikeLabel" runat="server" Text="Label"></asp:Label>
<br />
<div class="btn-group">
<asp:Button ID="LikeButton" runat="server" CssClass="btn btn-default" Text="Like"
OnClick="LikeButton_Click" /><asp:Button ID="UnlikeButton" CssClass="btn btn-default"
runat="server" Text="Dislike" OnClick="UnlikeButton_Click" />
</div>
</div>
</div>
</div>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="LikeButton" />
<asp:AsyncPostBackTrigger ControlID="UnlikeButton" />
</Triggers>
</asp:UpdatePanel>
this is my cs page
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 Question : System.Web.UI.Page
{
ConnectionClass cl;
DataTable dt,dt1;
string QuestonId;
protected void Page_Load(object sender, EventArgs e)
{
cl = new ConnectionClass();
QuestonId = Request.QueryString["Id"];
string[] var = { "@id" };
SqlDbType[] type = { SqlDbType.Int };
string[] value = { QuestonId };
dt1 = cl.DatatableProcedure("GetQuestionLikeDislike", var, type, value);
LikeLabel.Text = dt1.Rows[0].ItemArray[0].ToString();
DislikeLabel.Text = dt1.Rows[0].ItemArray[1].ToString();
dt = cl.DatatableProcedure("GetAnswers", var, type, value);
}
protected void LikeButton_Click(object sender, EventArgs e)
{
string[] var = { "@id" };
SqlDbType[] type = { SqlDbType.Int };
string[] value = { QuestonId };
dt1 = cl.DatatableProcedure("InsertLike&getLikeDislike", var, type, value);
VotePanel.Update();
}
protected void UnlikeButton_Click(object sender, EventArgs e)
{
string[] var = { "@id" };
SqlDbType[] type = { SqlDbType.Int };
string[] value = { QuestonId };
dt1 = cl.DatatableProcedure("InsertUnlike&getLikeDislike", var, type, value);
VotePanel.Update();
}
}
When I run this page and press on like button, it implements it two times and like is incremented by 2. Please help me with the solution so that the like is only incremented by one. That will be a great favor. Thanks in advance! these are my stored procedures:
ALTER procedure [dbo].[InsertUnlike&getLikeDislike]
(
@id int
)
as
begin
update QuestionTable
set Dislikes=Dislikes + 1 where QuestionId=@id;
select Likes, Dislikes from QuestionTable where QuestionId=@id
end
ALTER procedure [dbo].[InsertLike&getLikeDislike]
(
@id int
)
as
begin
update QuestionTable
set Likes=Likes + 1 where QuestionId=@id;
select Likes, Dislikes from QuestionTable where QuestionId=@id
end
ALTER procedure [dbo].[GetQuestionLikeDislike]
(
@id int
)
as
begin
select Likes, Dislikes from QuestionTable where QuestionId=@id
end
I updated my cs page to
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 Question : System.Web.UI.Page
{
ConnectionClass cl;
DataTable dt,dt1;
string QuestonId;
protected void Page_Load(object sender, EventArgs e)
{cl = new ConnectionClass();
QuestonId = Request.QueryString["Id"];
QuestonId = "1";
string[] var = { "@id" };
SqlDbType[] type = { SqlDbType.Int };
string[] value = { QuestonId };
if (!IsPostBack)
{
dt = cl.DatatableProcedure("GetQuestionDetail", var, type, value);
StatementLabel.Text = dt.Rows[0].ItemArray[0].ToString();
MemberNameLabel.Text = cl.ReturnNameFromId(dt.Rows[0].ItemArray[4].ToString()).ToString();
DateLabel.Text = dt.Rows[0].ItemArray[5].ToString();
TagsLabel.Text = dt.Rows[0].ItemArray[3].ToString();
ProblemLabel.Text = dt.Rows[0].ItemArray[1].ToString();
LikeDislike();
TriedLabel.Text = dt.Rows[0].ItemArray[2].ToString();
dt = cl.DatatableProcedure("GetAnswers", var, type, value);
drawAnswers();
}
}
public void LikeDislike()
{
string[] var = { "@id" };
SqlDbType[] type = { SqlDbType.Int };
string[] value = { QuestonId };
dt1 = cl.DatatableProcedure("GetQuestionLikeDislike", var, type, value);
LikeLabel.Text = dt1.Rows[0].ItemArray[0].ToString();
DislikeLabel.Text = dt1.Rows[0].ItemArray[1].ToString();
}
public void drawAnswers()
{
Label lb = new Label();
if(dt.Rows.Count==1)
lb.Text = dt.Rows.Count.ToString() +" Answer";
else
lb.Text = dt.Rows.Count.ToString() + " Answer";
AnswerPlaceHolder.Controls.Add(lb);
for (int i = 0; i < dt.Rows.Count; i++)
{
System.Web.UI.HtmlControls.HtmlGenericControl createDiv =
new System.Web.UI.HtmlControls.HtmlGenericControl("DIV");
createDiv.ID = "createDiv" + i.ToString();
Label[] l = new Label[5];
for (int j = 0; j < 5; j++)
{
l[j] = new Label();
l[j].Text = dt.Rows[i].ItemArray[j].ToString();
createDiv.Controls.Add(l[j]);
}
AnswerPlaceHolder.Controls.Add(createDiv);
}
}
protected void LikeButton_Click(object sender, EventArgs e)
{
string[] var = { "@id" };
SqlDbType[] type = { SqlDbType.Int };
string[] value = { QuestonId };
cl.executeProcedure("InsertLike", var, type, value);
LikeDislike();
}
protected void UnlikeButton_Click(object sender, EventArgs e)
{
string[] var = { "@id" };
SqlDbType[] type = { SqlDbType.Int };
string[] value = { QuestonId };
cl.executeProcedure("InsertUnlike", var, type, value);
LikeDislike();
}
}
procedures to
ALTER procedure [dbo].[InsertUnlike&getLikeDislike]
(
@id int
)
as
begin
update QuestionTable
set Dislikes=Dislikes + 1 where QuestionId=@id;
select Likes, Dislikes from QuestionTable where QuestionId=@id
end
ALTER procedure [dbo].[InsertLike&getLikeDislike]
(
@id int
)
as
begin
update QuestionTable
set Likes=Likes + 1 where QuestionId=@id;
select Likes, Dislikes from QuestionTable where QuestionId=@id
end
ALTER procedure [dbo].[GetQuestionLikeDislike]
(
@id int
)
as
begin
select Likes, Dislikes from QuestionTable where QuestionId=@id
end
and removed ChildAsTriggers property from update panel. It worked! :)