|
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 <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.
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.
|
|
|
|
|
AspDotNetDev wrote: Language="vb"
There's your problem...
Okay, I admit it, that was another feeble dig at vb...
|
|
|
|
|
|
I know I know I know.......
Did you not see the joke icon and apology for the traditional poor dig at VB.....I can't even believe you replied telling me.
|
|
|
|
|
DaveAuld wrote: I admit it, that was another feeble dig at vb... This is The Lounge. We don't discriminate against people that make fun of VB, ... only people that use VB.
The United States invariably does the right thing, after having exhausted every other alternative. -Winston Churchill
America is the only country that went from barbarism to decadence without civilization in between. -Oscar Wilde
Wow, even the French showed a little more spine than that before they got their sh*t pushed in.[^] -Colin Mullikin
|
|
|
|
|
No worries, it's practically obligatory. I think I got 4 "you're using VB" responses to the last rant I posted that was related to VB.
|
|
|
|
|
Hey, Visual Basic has some strong points, like the Application Framework system and a lot of useful utility methods, like a method to send a file to the recycle bin instead of outright deleting it.
Quote: It is a poor workman who blames his tools.
With Option Strict on, VB is a rather safe language to use.
Don't get me wrong, I usually use C# now, but VB still has its place. And that place is NOT in the recycle bin!
Bob Dole The internet is a great way to get on the net.
2.0.82.7292 SP6a
|
|
|
|
|
The irony of someone called AspDotNetDev complaining about ASP.NET.
|
|
|
|
|
My first thought as well.
The United States invariably does the right thing, after having exhausted every other alternative. -Winston Churchill
America is the only country that went from barbarism to decadence without civilization in between. -Oscar Wilde
Wow, even the French showed a little more spine than that before they got their sh*t pushed in.[^] -Colin Mullikin
|
|
|
|
|
I typically avoid complaining about things I am ignorant of.
|
|
|
|
|
It's been a while and I'm not in front of a computer _ I rarely create asp from scratch anyway - but don't you ALWAYS need to check whether it's post back or not?
Ok, granted, asp error message isn't the most intuitive ! I sometimes think it would be better not to get anything above 'something went wrong' rather than verbose messages that, essentially, tell you something went wrong!
|
|
|
|
|
_Maxxx_ wrote: don't you ALWAYS need to check whether it's post back or not?
Nope, only when there's a purpose to doing so. You may always want the most recent data to be displayed on each postback.
|
|
|
|
|
Yep - I phrased that badly; my code normally has an if..else around postback - as a template almost - so I do 'non first time; code inside IsPostback and other code inside Else.
I can see what you mean, though - if you don't want to have initialization stuff because you always want to update to the latest data.
Of course, you DON'T want to bind to controls except on the initial build of the page ;o
|
|
|
|
|
I would also generally move the databinding to Page_PreRender instead of Page_Load.
If you have a datasource that's filtered by user input you want all the control events to have happened before you databind.
People say nothing is impossible, but I do nothing every day.
|
|
|
|
|
Stick it in CP as a tip or trick. That way, the next person to have the problem will find your solution.
I wanna be a eunuchs developer! Pass me a bread knife!
|
|
|
|