Gunnar Peipman's ASP.NET blog

ASP.NET, C#, SharePoint, SQL Server and general software development topics.

Sponsors

News

 
 
 
 
 
Programming Blogs - Blog Catalog Blog Directory
 
 
 

Links

Social

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.


kick it on DotNetKicks.com pimp it Shout it

Comments

Anıl Çalışkan said:

<h5>Firma</h5>?

so your project is in turkish? :-)

Thanks for the tip!

# May 10, 2009 4:41 PM

DigiMortal said:

No, it is not turkish. It is estonian language. Firma means company. :)

# May 11, 2009 2:11 AM

DotNetShoutout said:

Thank you for submitting this cool story - Trackback from DotNetShoutout

# May 21, 2009 2:36 PM

DotNetKicks.com said:

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# May 21, 2009 2:38 PM

DotNetBurner - ASP.net said:

DotNetBurner - burning hot .net content

# May 21, 2009 2:39 PM

PimpThisBlog.com said:

Thank you for submitting this cool story - Trackback from PimpThisBlog.com

# May 21, 2009 2:40 PM

vivek.kumar@proteans.com said:

Just add this to your web config and make pages..like ErrorPage.aspx , and see you will control this error by making your custom control error.

<system.web>

<customErrors defaultRedirect="ErrorPage.aspx"mode="On">       <error statusCode="500" redirect="servererror.aspx" />    <error statusCode="404" redirect="filenotfound.aspx"/>    <error statusCode="403" redirect="AccessDenied.aspx"/> </customErrors>

<system.web>

# May 26, 2009 6:40 AM

DigiMortal said:

Thanks for your feedback, Vivek. Using error pages doesn't solve the problem - problem still appears but instead of handling it we will redirect user away and tell him that system has problem.

My solution here solves the problem so I don't have to interrupt activities that users are doing. Situation is handled nicely and nothing is broken. :)

# May 26, 2009 6:58 AM

Wolverine said:

Thanks,

 I was about to start pulling my hair out because of this error.   Just had to uniquely name and ID all my controls.  Seems like when there are too many similarly named controls on the page that error pops up.

# June 23, 2009 8:11 AM

Amr Ellafi said:

the issue may raise if you execute the DataBind() Method for the repeater on the PageLoad event because it will re-assign new control IDs before reaching the ItemCommand event for the repeater control. you should call DataBind() with a conditional if (!IsPostBack), however I'm not quite sure about this issue in SharePoint

# June 28, 2009 8:46 PM

Chuck van der Linden said:

If we are talking about the ID attributes of your controls in the page HTML, then remember that per the HTML specs, ID attributes should always be unique anyway..  it's invalid HTML to have the same ID appear more than once on a page.   Name can be repeated, and Class but not ID.

Also be aware that when/if you try to create browser level automation to test the site, it's a lot easier for whoever has to do that to properly idenfify instances of controls if they have some unique proper (such as id) instead of repeating all the same attribute values for the elements on the page.

# July 31, 2009 2:18 PM

Dele said:

This was extremely helpful.  Thank you for posting this

# September 2, 2009 12:58 PM

TJ said:

I was trying to change the add cusom webpart of the shared page of the sharepoint site. After adding a webpart i got a screen where i selected all the web-parts and after that the startpage of sharepoint site did not contain any webparts anymore. When adding new webparts to the startpage of sharepoint site the following error occurred.

Server Error in '/' Application.

----------------------------------------------

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.

# September 28, 2009 5:37 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)