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.)
 

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

Top