Click here to Skip to main content
12,510,224 members (54,982 online)

Welcome to the Lounge

   

For discussing anything related to a software developer's life. Technical discussions are encouraged, but click here to ask your programming questions.

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, no programming questions and please don't post ads.
 
GeneralAnother Day Wasted By ASP.NET's Useless Error Messages Pin
AspDotNetDev21-Jan-13 8:44
protectorAspDotNetDev21-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


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 Pin
DaveAuld21-Jan-13 8:49
mentorDaveAuld21-Jan-13 8:49 
GeneralMessage Removed Pin
_beauw_21-Jan-13 9:05
member_beauw_21-Jan-13 9:05 
GeneralRe: Another Day Wasted By ASP.NET's Useless Error Messages Pin
DaveAuld21-Jan-13 9:07
mentorDaveAuld21-Jan-13 9:07 
GeneralRe: Another Day Wasted By ASP.NET's Useless Error Messages Pin
Colin Mullikin21-Jan-13 10:16
memberColin Mullikin21-Jan-13 10:16 
GeneralRe: Another Day Wasted By ASP.NET's Useless Error Messages Pin
AspDotNetDev21-Jan-13 11:51
protectorAspDotNetDev21-Jan-13 11:51 
GeneralRe: Another Day Wasted By ASP.NET's Useless Error Messages Pin
Brisingr Aerowing21-Jan-13 13:55
memberBrisingr Aerowing21-Jan-13 13:55 
GeneralRe: Another Day Wasted By ASP.NET's Useless Error Messages Pin
Pete O'Hanlon21-Jan-13 9:53
protectorPete O'Hanlon21-Jan-13 9:53 
GeneralRe: Another Day Wasted By ASP.NET's Useless Error Messages Pin
Colin Mullikin21-Jan-13 10:21
memberColin Mullikin21-Jan-13 10:21 
GeneralRe: Another Day Wasted By ASP.NET's Useless Error Messages Pin
AspDotNetDev21-Jan-13 11:49
protectorAspDotNetDev21-Jan-13 11:49 
GeneralRe: Another Day Wasted By ASP.NET's Useless Error Messages Pin
_Maxxx_21-Jan-13 10:01
member_Maxxx_21-Jan-13 10:01 
GeneralRe: Another Day Wasted By ASP.NET's Useless Error Messages Pin
AspDotNetDev21-Jan-13 11:52
protectorAspDotNetDev21-Jan-13 11:52 
GeneralRe: Another Day Wasted By ASP.NET's Useless Error Messages Pin
_Maxxx_21-Jan-13 13:18
member_Maxxx_21-Jan-13 13:18 
GeneralRe: Another Day Wasted By ASP.NET's Useless Error Messages Pin
Jörgen Andersson21-Jan-13 10:06
memberJörgen Andersson21-Jan-13 10:06 
GeneralRe: Another Day Wasted By ASP.NET's Useless Error Messages Pin
Mark_Wallace22-Jan-13 1:44
memberMark_Wallace22-Jan-13 1:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    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.160929.1 | Last Updated 30 Sep 2016
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid