Invalid postback or callback argument error in SharePoint Visual WebPart

With the introduction of SharePoint Visual WebPart in Visual Studio 2010 it is a lot easier to develop en deploy a custom WebPart. Last I developed a WebPart that uses a custom webusercontrol. That control contains some logic and a linkbutton. In the Visual WebPart the custom webusercontrol is used in a repeater control. So I do not know how many of those will be created at runtime. When I clicked the linkbutton the following error occurred:

Invalid postback or callback argument.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Read more about the nature of this error.

When you google around you see that the solution is to register the control that initiates the event or callback. You must use the ClientScript.RegisterForEventValidation method to do this.

That was the tricky part. I ‘ve tried that but it didn’t work at all. How comes? Well, it works fine with a plain ASP.Net application but in a SharePoint WebPart you have to put it in the actual webpart code file. Not in the Visual WebPart code file.

So now it looks like this:

image