通过更新面板更新时在我的页面上获得两次
getting two times like on my page when updating through update panel
我正在 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。
请帮我解决这个问题,这样 like 只加一。那将是一个很大的帮助。提前致谢!
这些是我的存储过程:
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
如下更改您的页面加载方法,
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
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);
}
}
我将我的 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"];
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();
}
}
程序
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
并从更新面板中删除了 ChildAsTriggers 属性。有效! :)
我正在 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。 请帮我解决这个问题,这样 like 只加一。那将是一个很大的帮助。提前致谢! 这些是我的存储过程:
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
如下更改您的页面加载方法,
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
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);
}
}
我将我的 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"];
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();
}
}
程序
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
并从更新面板中删除了 ChildAsTriggers 属性。有效! :)