Click here to Skip to main content

Welcome to the Lounge

   

For lazing about and discussing anything in a software developer's life that takes your fancy.
The Lounge is rated PG. If you're about to post something you wouldn't want your kid sister to read then don't post it. No flame wars, no abusive conduct and please don't post ads.

Technical discussions are welcome, but if you need specific help please use the programming forums.


 
GeneralAnother Day Wasted By ASP.NET's Useless Error Messages PinprotectorAspDotNetDev21-Jan-13 8:44 
Try running this code in an ASPX page (and click the button once the page loads):
<%@ Page Language="vb" AutoEventWireup="false" %>
 
<script runat="server">
  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    lvTest.DataSource = New List(Of String)() From {"hello"}
    lvTest.DataBind()
  End Sub
 
  Protected Sub btnTest_Click(sender As Object, e As System.EventArgs)
    lblTest.Text = DateTime.Now.ToString()
  End Sub
</script>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Testing Postback Error</title>
</head>
<body>
    <form id="frmMain" runat="server">
      <div>
        <asp:Repeater runat="server" ID="lvTest">
          <ItemTemplate>
            <asp:Button runat="server" ID="btnTest" Text="Test Postback" onclick="btnTest_Click" />
          </ItemTemplate>
        </asp:Repeater>
        <asp:Label runat="server" ID="lblTest" />
      </div>
    </form>
</body>
</html>
 
You'll get an informative and useful error message like this:
Invalid postback or callback argument. Event validation is enabled using 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.

 
Everywhere I searched online suggested I add EnableEventValidation="false" to the page declaration, which is really just covering up the problem. I eventually discovered the real fix:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  If Not Page.IsPostBack Then
    lvTest.DataSource = New List(Of String)() From {"hello"}
    lvTest.DataBind()
  End If
End Sub
 
It seems that binding on a postback in the page load (before the click handlers are called) confuses ASP.NET (I suppose because the control posting back gets replaced when binding occurs). The fix is to not do that. If I had more motivation, I'd go through all those online posts and show them the actual fix. Roll eyes | :rolleyes:

JokeRe: Another Day Wasted By ASP.NET's Useless Error Messages PinmentorDaveAuld21-Jan-13 8:49 
GeneralMessage Removed Pinmember_beauw_21-Jan-13 9:05 
GeneralRe: Another Day Wasted By ASP.NET's Useless Error Messages PinmentorDaveAuld21-Jan-13 9:07 
GeneralRe: Another Day Wasted By ASP.NET's Useless Error Messages PinmemberColin Mullikin21-Jan-13 10:16 
GeneralRe: Another Day Wasted By ASP.NET's Useless Error Messages PinprotectorAspDotNetDev21-Jan-13 11:51 
GeneralRe: Another Day Wasted By ASP.NET's Useless Error Messages PinmemberBrisingr Aerowing21-Jan-13 13:55 
GeneralRe: Another Day Wasted By ASP.NET's Useless Error Messages PinprotectorPete O'Hanlon21-Jan-13 9:53 
GeneralRe: Another Day Wasted By ASP.NET's Useless Error Messages PinmemberColin Mullikin21-Jan-13 10:21 
GeneralRe: Another Day Wasted By ASP.NET's Useless Error Messages PinprotectorAspDotNetDev21-Jan-13 11:49 
GeneralRe: Another Day Wasted By ASP.NET's Useless Error Messages Pinmember_Maxxx_21-Jan-13 10:01 
GeneralRe: Another Day Wasted By ASP.NET's Useless Error Messages PinprotectorAspDotNetDev21-Jan-13 11:52 
GeneralRe: Another Day Wasted By ASP.NET's Useless Error Messages Pinmember_Maxxx_21-Jan-13 13:18 
GeneralRe: Another Day Wasted By ASP.NET's Useless Error Messages PinmemberJörgen Andersson21-Jan-13 10:06 
GeneralRe: Another Day Wasted By ASP.NET's Useless Error Messages PinmemberMark_Wallace22-Jan-13 1:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.


Advertise | Privacy | Mobile
Web02 | 2.8.140709.1 | Last Updated 11 Jul 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid