2012-09-16 126 views
4

我得到一個按鈕下面的錯誤點擊與GridView控件錯誤:無效的回發或回調參數

Server Error in '/' Application. 
Invalid postback or callback argument. Event validation is enabled using <pages enableEventValidation="true"/> in configuration or <%@ Page EnableEventValidation="true" %> in a page. For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them. If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation. 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.ArgumentException: Invalid postback or callback argument. Event validation is enabled using <pages enableEventValidation="true"/> in configuration or <%@ Page EnableEventValidation="true" %> in a page. For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them. If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation. 

Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below. 

Stack Trace: 


[ArgumentException: Invalid postback or callback argument. Event validation is enabled using <pages enableEventValidation="true"/> in configuration or <%@ Page EnableEventValidation="true" %> in a page. For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them. If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.] 
    System.Web.UI.ClientScriptManager.ValidateEvent(String uniqueId, String argument) +144 
    System.Web.UI.Control.ValidateEvent(String uniqueID, String eventArgument) +111 
    System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +29 
    System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10 
    System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13 
    System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +35 
    System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1724 


Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.17929 

當我按gridview的內部按鈕,奇怪的是,我有另外一個GridView控件它發生,也可以在運行不同代碼的列中使用自定義按鈕,但不會出現錯誤。以下是該頁面和代碼隱藏的代碼。

namespace CCCC 
{ 
    public partial class drivermangement : System.Web.UI.MasterPage 
    { 

     protected void Page_Load(object sender, EventArgs e) 
     { 
      if (System.Web.HttpContext.Current.User.Identity.IsAuthenticated) 
      { 
       if (Roles.IsUserInRole("Administrator")) 
       { 
        LoggedInUser.Value = Convert.ToString(Request.QueryString["driver"]); 
       } 
       else 
       { 
        LoggedInUser.Value = Membership.GetUser().UserName.ToString(); 
       } 
       DayOfTheWeekHiddenField.Value = Convert.ToString(Request.QueryString["dow"]); 
      } 
      else 
      { 
       Response.Redirect("default.aspx"); 
      } 
      if (NewCustomersGrid.Rows.Count == 0) 
      { 
       NewCustomersLabel.Visible = false; 
      } 
      else 
      { 
       NewCustomersLabel.Visible = true; 
      } 
      if (NeedCompostGrid.Rows.Count == 0) 
      { 
       NeedCompostLabel.Visible = false; 
      } 
      else 
      { 
       NeedCompostLabel.Visible = true; 
      } 
      if (CanceledGrid.Rows.Count == 0) 
      { 
       CanceledLabel.Visible = false; 
      } 
      else 
      { 
       CanceledLabel.Visible = true; 
      } 
      if (VacationGrid.Rows.Count == 0) 
      { 
       VacationLabel.Visible = false; 
      } 
      else 
      { 
       VacationLabel.Visible = true; 
      } 
      if (NewCustomersGrid0.Rows.Count == 0) 
      { 
       NewCustomersLabel0.Visible = false; 
      } 
      else 
      { 
       NewCustomersLabel0.Visible = true; 
      } 
      if (NeedCompostGrid0.Rows.Count == 0) 
      { 
       NeedCompostLabel0.Visible = false; 
      } 
      else 
      { 
       NeedCompostLabel0.Visible = true; 
      } 
      if (CanceledGrid0.Rows.Count == 0) 
      { 
       CanceledLabel0.Visible = false; 
      } 
      else 
      { 
       CanceledLabel0.Visible = true; 
      } 
     } 

     protected void NewCustomerDoneButton_Click(object sender, EventArgs e) 
     { 
      int CustomerID = Convert.ToInt32(((Button)sender).CommandArgument); 
      string CustomerBinNeedAcknowledged = "Yes"; 
      string strConnString = "Data Source"; 
      using (SqlConnection con = new SqlConnection(strConnString)) 
      { 
       using (SqlCommand cmd = new SqlCommand()) 
       { 
        cmd.Connection = con; 
        cmd.CommandType = CommandType.Text; 
        cmd.CommandText = "UPDATE Customers SET [email protected] WHERE [email protected]"; 
        cmd.Parameters.AddWithValue("@CustomerBinNeedAcknowledged", CustomerBinNeedAcknowledged); 
        cmd.Parameters.AddWithValue("@CustomerId", CustomerID); 
        con.Open(); 
        cmd.ExecuteNonQuery(); 
        con.Close(); 
       } 
      } 
     } 

和實際的頁面:

<%@ Master Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" Inherits="CCCCCC.drivermangement" CodeBehind="drivermangement.master.cs" %> 
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server"> 
</asp:Content> 
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> 
    <asp:HiddenField ID="LoggedInUser" runat="server" /> 
    <asp:HiddenField ID="DayOfTheWeekHiddenField" runat="server" /> 
    <ajaxToolkit:TabContainer ID="RoutingTabs" runat="server" ActiveTabIndex="0" 
     Width="900px"> 
     <ajaxToolkit:TabPanel runat="server" HeaderText="Pre-Route" ID="PreRouteTab"> 
     <ContentTemplate> 
<br /> 
<asp:Label ID="NewCustomersLabel" runat="server" 
     style="font-weight: 700; font-size: large; color: #009933" Text="New Customers"></asp:Label> 

<asp:GridView ID="NewCustomersGrid" runat="server" AllowPaging="True" AutoGenerateColumns="False" BackColor="White" 
     BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" CellPadding="3" 
     DataKeyNames="CustomerId" DataSourceID="NewCustomers" ForeColor="Black" 
     GridLines="Vertical"> 
<AlternatingRowStyle BackColor="#99FF99" ></AlternatingRowStyle> 
<Columns> 
<asp:BoundField DataField="CustomerFullName" HeaderText="Name" 
       SortExpression="CustomerFullName" /> 
<asp:BoundField DataField="CustomerFullAddress" HeaderText="Address" 
       SortExpression="CustomerFullAddress" /> 
<asp:BoundField DataField="CustomerNeedsBin" HeaderText="Needs Bin?" 
     SortExpression="CustomerNeedsBin" /> 
<asp:TemplateField ShowHeader="False"><ItemTemplate> 
       <asp:Button ID="NewCustomerDoneButton" runat="server" CommandName="" 
       Text="Done" CommandArgument='<%# Eval("CustomerID") %>' OnClick="NewCustomerDoneButton_Click" CausesValidation="False" /> 

</ItemTemplate> 
</asp:TemplateField> 
</Columns> 
<asp:SqlDataSource ID="NewCustomers" runat="server" 
     ConnectionString="<%$ ConnectionStrings:tcc_customersConnectionString %>" 

       SelectCommand="SELECT [CustomerId], [CustomerStatus], [CustomerFullName], [CompanyName], [CustomerFullAddress], [CustomerPickUpDay], [CustomerPickUpDay2], [CustomerDriver], [CustomerNeedsBin], [CustomerBinNeedAcknowledged] FROM [Customers] WHERE (([CustomerBinNeedAcknowledged] = @CustomerBinNeedAcknowledged) AND ([CustomerNeedsBin] = @CustomerNeedsBin) AND ([CustomerDriver] = @CustomerDriver) AND ([CustomerStatus] = @CustomerStatus) AND ([CustomerPickUpDay] = @CustomerPickUpDay OR [CustomerPickUpDay2] = @CustomerPickUpDay2))"><SelectParameters> 
<asp:Parameter DefaultValue="No" Name="CustomerBinNeedAcknowledged" Type="String" /> 
<asp:Parameter DefaultValue="Yes" Name="CustomerNeedsBin" Type="String" /> 
<asp:ControlParameter ControlID="LoggedInUser" Name="CustomerDriver" 
      PropertyName="Value" Type="String"></asp:ControlParameter> 
<asp:Parameter DefaultValue="New" Name="CustomerStatus" Type="String" /> 
<asp:ControlParameter ControlID="DayOfTheWeekHiddenField" Name="CustomerPickUpDay" 
       PropertyName="Value" Type="String" ></asp:ControlParameter> 
<asp:ControlParameter ControlID="DayOfTheWeekHiddenField" 
      Name="CustomerPickUpDay2" PropertyName="Value" Type="String" /> 
</SelectParameters> 
</asp:SqlDataSource> 

</ContentTemplate> 

</ajaxToolkit:TabPanel> 
     <ajaxToolkit:TabPanel runat="server" HeaderText="Post-Route" ID="PostRouteTab"><ContentTemplate> 

現在我想知道,這是否有什麼關係的事實,GridViewAjax Tab Container裏面?因爲我的工作正常是不是其他表...

注:不得不削減從由於頁一些代碼字符數限制

+0

忘記了,我不想禁用事件驗證。它只解決問題(收益,但視圖沒有更新),並使我容易受到攻擊,因爲它是一種安全措施。 – cb1295

+1

請僅共享相關的代碼位;這可能是您反對票的原因 – NoviceProgrammer

+0

您使用的是更新面板嗎?你是否將數據動態添加到網格中? – NoviceProgrammer

回答

15

1)GridView中的Postback或Callback參數無效問題可能是:您正在Page_Load事件中使用Object Data Source或Manual Binding與函數調用綁定數據。這將使您的GridView綁定任何控件的每個事件觸發數據。

當您使用OnRowCommand觸發任何GridView命令時,在RowCommand觸發之前,您的GridView將重新綁定,並且其中的所有控件將分配給新的ID。因此RowCommand無法獲取激發事件的項目。

爲無效的回發或回調參數在GridView的解決方案:可以,如果條件

if (!IsPostBack) 
{ 
    //Your code for Bind data 
} 

內這個綁定您的數據該代碼肯定會給你的解決方案,如果這個不行再檢查其他任何控件是否是不會放棄錯誤。

+0

有關詳情,請訪問 http://www.aspnetajaxtutorials.com/2007/10/invalid-postback-or-callback-argument.html 保護無效的Page_Load(對象發件人,EventArgs的){ 如果 (Page.IsPostBack!) {// 您的代碼在這裏 }} 即使它不工作,去爲這個鏈接 HTTP:/ /blogs.msdn.com/b/carloc/archive/2008/05/02/invalid-postback-or-callback-argument-in-asp-net.aspx – Pushpendra

+0

謝謝,這樣做的伎倆 – cb1295

1

你更新電網或通過JavaScript或Ajax的任何這樣的控制。

如果是這種情況,那麼你可能會面對這一點。可能的解決方案可能是將EnableEventValidation設置爲false。

+0

沒有。這些都不和我不想EnableEventValidation設置爲false,出於安全原因 – cb1295

0

有幾件事情來嘗試

  1. 在你Page Load情況下,添加一個檢查!Page.IsPostBack和移動非認證代碼塊存在。
  2. 使用LinkButton而不是常規按鈕進行實驗。
0

這可能是因爲您正在使用任何第三方控件。

我也有這個錯誤。在我的情況下,我有一箇中繼器控制內updatepanel和內部中繼器控制我使用telerik datepicker控制。它在IE中工作正常,但不是在Mozilla和Chrome中。我通過從chrome和IE發送請求來檢查兩種情況下的表單數據,發現觸發控制值稍有不同。我使用telerik RadAjaxmanager而不是updatepanel,錯誤消失了。

相關問題