通过更新面板进行更新时,在我的页面上得到两次

时间:2016-04-15 08:52:58

标签: c# asp.net updatepanel

我在我的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 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

2 个答案:

答案 0 :(得分:2)

更改您的页面加载方法,如下所示

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);         
    }       
}

答案 1 :(得分:0)

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! :)