Invalid postback or callback argument

G

Guest

I have a repeater with and imagebutton on a page useing VS2005 ASP.Net 2.0

<asp:Repeater ID="Repeater1" runat="server" >
<ItemTemplate>
<div>
<asp:ImageButton ImageUrl="button.gif" ID="ImageButton1"
runat="server" />
<p><%# Eval("Name") %></p>
</div>
</ItemTemplate>
</asp:Repeater>

if I add an SqlDataSource to the page and bind declaratively no errors Are
generated

if however I bind a data source to the repeater in code,

ConnectionStringSettings settings;
settings = ConfigurationManager.ConnectionStrings["csWebDataV3"];
SqlConnection sqlCONN = new SqlConnection();
DataSet DS = new DataSet();

sqlCONN.ConnectionString = settings.ConnectionString;
SqlCommand sqlCMD = new SqlCommand();
sqlCMD.Connection = sqlCONN;
sqlCMD.CommandText = "SELECT Name FROM DevelopmentInfo";

sqlCMD.CommandType = CommandType.Text;

SqlDataAdapter ad = new SqlDataAdapter();
ad.SelectCommand = sqlCMD;

sqlCONN.Open();
ad.Fill(DS);
sqlCONN.Close();

Repeater1.DataSource = DS;
Repeater1.DataBind();

the I get the following error:

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.

could this be a bug? or am I missing somthing.

I know if you add EnableEventValidation="false" to the page directive the
error goes away.

can anyone help or point me in the riht direction.
 
S

Steven Cheng[MSFT]

Hi wizzard,

Thank you for posting in the ASPNET newsgroup.

As for the "EventValidation" exception you encountered, it is due to a new
validation feature in ASP.NET 2.0 which will check the postback event and
data's source in each postback request(this is enabled by default for
ASP.NET 2.0 application and pages). The "EventValidation" will check the
postback event and postback data to make sure they're triggered by or from
the existing ASP.NET server controls (not any dynamically created client
elements or script through cilent scripts .....) on the web page.

Then, for your scenario, your repeater page have the following things which
cause the eventvalidation exception happen:

#1. The repeater control's ItemTemplate contain a Image server control.
when clicking in the Image at client-side, the page will postback.

#2. You originally put the repeater control's databind code in Page_load
and without check the Page.IsPostBack

Then, #2 cause the repeater control's repeaterItems(also sub templated
controls , include the Image) be recreated everytime the page is postback.
This cause the postback event triggered by a certain Image in the repeater
item can not be associated to its original Image control (since the
repeater's control/item collection is regenerated through databinding).
Then, the runtime think the postback is caused by an unknown source and
throw the "EventValidation" Exception.

And after you wrapper the databinding code with If(!IsPostBack) check, the
problem is fixed. In addition, there is a good blog article introducing
the event validation feature in ASP.NET 2.0:


#ASP.NET Event Validation and ¡°Invalid Callback Or Postback Argument¡± :
Part I
http://odetocode.com/Blogs/scott/archive/2006/03/20/3145.aspx

Hope this also helps clarify this. If there is still anything unclear or
any other questions on this, please feel free to post here.

Regards,

Steven Cheng
Microsoft MSDN Online Support Lead


==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================


This posting is provided "AS IS" with no warranties, and confers no rights.



Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
Joined
Jan 7, 2008
Messages
1
Reaction score
0
The reason for this error

Taking that you are using a repeater control that consists of a few labels and an action control like a image button, either set EnableViewstate of the repeater to false and repopulate the it after every postback, or place the populate code inside the IsPostBack = False statement.

What happens here:
When populating the repeater every time you you do a postback with EnableViewstate = True, the engin generates a new unique id for the event validation every time where the previously generated repeater does not match up with the newly generated repeater.

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

If Not IsPostBack Then
With rptInProcess
.DataSource = ds
.DataBind()
End With
End If
End Sub
 
Joined
Mar 19, 2009
Messages
1
Reaction score
0
another solution

if you need to aways databind the source even on postbacks, if you use the same id and stuff the error will appear(i do this most of times because I don't use sql connections, only arraylists or lists<>, so I aways need to databind)
what I did was putting enableviewstate=false at the repeater and it worked
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,967
Messages
2,570,148
Members
46,694
Latest member
LetaCadwal

Latest Threads

Top