Invalid postback or callback argument. Event validation is enabled using in configuration

I wrote web part that uses Repeater control to create some repeating blocks of output. Each of these blocks has Button control in it. When I ran web part under SharePoint I got 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. Here is the solution.

Button control in Repeater item template has ID assigned to it. If Repeater data source has more than one row then this exception is thrown. My solution was simple. As I had ItemDataBound event handler written anyway I solved the problem there. All I had to do was to change Button control ID so it is unique. Here is my repeater (actually, very-very simplified version of it).


<asp:Repeater runat="server" ID="companiesRepeater"> 
<ItemTemplate>
 
   
<div class="info-row">
 
       
<div class="title">
 
           
<h5>Firma</h5>
 
       
</div>
 
       
<div class="information-main">
 
           
<asp:Label runat="server" ID="companyName"></asp:Label>
 
       
</div>
 
   
</div>
 
   
<div class="submit" runat="server" id="buttonContainer">
 
       
<asp:Button runat="server" ID="editCompanyButton" Text="Edit"></asp:Button>
 
   
</div> 
</ItemTemplate>               
</asp:Repeater
>

To bind repeater with data I am using code like this.


public void BindCompanies()
{
   
var list = SPContext.Current.Site.OpenWeb("/companies/").Lists["Pages"
];

   
var queryString = "<Where><Eq><FieldRef Name='Author' />"
;
    queryString +=
"<Value Type='User'>{0}</Value></Eq></Where>"
;
    queryString =
string
.Format(queryString, Item.Title);

   
var query = new SPQuery
();
    query.Query = queryString;

   
var
items = list.GetItems(query);
   
if (items == null
)
       
return;

    companiesRepeater.DataSource = items;
    companiesRepeater.DataBind();
}

When I try to bind repeater to data then everything is okay. Error occurs when I try to push editCompanyButton because buttons in ItemTemplate have similar ID-s. My solution here simple one and it works. At least for me.


protected void companiesRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
   
if (e.Item.DataItem == null
)
       
return
;

   
var
item = (SPListItem)e.Item.DataItem;

   
// uninteresting code

   
var button = (Button)e.Item.FindControl("editCompanyButton"
);
    button.CommandArgument = item.ID.ToString();
    button.ID =
"button_" + e.Item.ItemIndex;
    button.Click += editCompanyButton_Click;
}

As you can see the solution is simple: each button in item template has now different ID and error mentioned above doesn’t occur anymore.


Leave a Reply

Your email address will not be published. Required fields are marked *