2016-04-15 40 views
2

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

你爲什麼要手動調用'VotePanel.Update()'?更新將由按鈕點擊無論如何,與這個調用你更新它兩次我認爲 – Andrei

+0

@Anderi,我從我的代碼刪除了這一行,但仍然工作相同 –

回答

0

我更新了我的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 [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 

並從更新面板中刪除了ChildAsTriggers屬性。有效! :)

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

當我這樣做,然後更新面板不工作所有@Piyush –

+0

它應該工作。你可以調試和檢查按鈕的事件是否被調用? –

+0

請告訴我還有什麼我可以做 –

相關問題