2016-04-06 24 views
0

首先,我接收到錯誤消息增加數據庫值Asp.Net C#

無效回發或回調參數。事件驗證在配置中啓用,或在頁面中啓用<%@ Page EnableEventValidation =「true」%>。爲了安全起見,此功能驗證回發或回調事件的參數來自最初呈現它們的服務器控件。如果數據有效且預期,請使用ClientScriptManager.RegisterForEventValidation方法爲註冊回發或回調數據進行驗證。

我正在使用重複器在數據庫中創建許多事件,並且每個事件都包含EvtName,EvtType,EvtDescription,EvtDate和EvtVote。 EvtVote以int形式存儲,並且我有一個名爲「eventButton」的按鈕,每次點擊時,EvtVote都應該更新一次。但是,當我單擊該按鈕時,會顯示錯誤消息。這是創建這個的代碼。

<asp:Repeater runat="server" ID="repeaterEvent"> 
    <ItemTemplate> 
     <div class="jumbotron"> 

      <h2><asp:Label ID="lblEventTest" runat="server" Text='<%#Bind("EvtName") %>'></asp:Label></h2> 
      <h3><asp:Label ID="Label1" runat="server" Text='<%#Bind("EvtType") %>'></asp:Label></h3> 
      <h4><asp:Label ID="Label3" runat="server" Text='<%#Bind("EvtDate") %>'></asp:Label></h4> 
      <h4><asp:Label ID="Label2" runat="server" Text='<%#Bind("EvtDescription") %>'></asp:Label></h4> 
      <h4><asp:Label runat="server">Amount Attending: </asp:Label>       
      <asp:Label ID="Label4" runat="server" Text='<%#Bind("EvtVote") %>'></asp:Label></h4> 
      <asp:Button runat="server" ID="eventButton" Text="Attending" class="btn btn-primary" OnClick="EventVote_Click"/> 
     </div> 
    </ItemTemplate> 
</asp:Repeater> 

這裏是按鈕的後端代碼。

public void EventVote_Click(object sender, EventArgs e) 
{ 
    SqlCommand cmd = new SqlCommand("UPDATE Events SET EvtVote = EvtVote + 1"); 
    loadDatabase(cmd); 
} 

不知道錯誤信息是什麼,但任何建議都會很好。

+1

這不是你的錯誤的原因,但我要指出,你的查詢將增加1每一個事件的投票,不僅與該按鈕相關聯的一個事件。 –

+0

好的,謝謝你。我還沒有足夠的時間來測試查詢,但我不得不考慮一種解決方法。謝謝 – Stuart

+0

綁定'repeaterEvent'如果不回發 –

回答

0

爲了解決這個問題,我添加的代碼:

EnableEventValidation="false" 

到我的網頁指令。現在看起來是這樣的:

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Events.aspx.cs" Inherits="Website.Events" EnableEventValidation="false"%>