我在我的asp.net頁面上實現了喜歡和不喜歡。這裏是aspx
頁通過更新面板更新時得到兩次喜歡我的網頁
<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>
這是我的CS
頁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();
}
}
當我像按鈕來運行這個頁面,然後按,它實現了它兩次,喜歡被2 請幫助增加我與解決方案,以便類似只增加一。這將是一個很大的忙。提前致謝! 這些都是我的存儲過程:
ALTER procedure [dbo].[InsertUnlike&getLikeDislike]
(
@id int
)
as
begin
update QuestionTable
set Dislikes=Dislikes + 1 where [email protected];
select Likes, Dislikes from QuestionTable where [email protected]
end
ALTER procedure [dbo].[InsertLike&getLikeDislike]
(
@id int
)
as
begin
update QuestionTable
set Likes=Likes + 1 where [email protected];
select Likes, Dislikes from QuestionTable where [email protected]
end
ALTER procedure [dbo].[GetQuestionLikeDislike]
(
@id int
)
as
begin
select Likes, Dislikes from QuestionTable where [email protected]
end
你爲什麼要手動調用'VotePanel.Update()'?更新將由按鈕點擊無論如何,與這個調用你更新它兩次我認爲 – Andrei
@Anderi,我從我的代碼刪除了這一行,但仍然工作相同 –