While working with an asp.net page in SharePoint 2007 (or, in any other situation where you get this error), when you try to bind the OnClick event of a button to a code behind event receiver, you will get an error saying that you are not allowed to directly access the OnClick event of the control.
There are proposed solutions on the web, some of which deal with the issue by modifying the web.config file.
Generally speaking, I prefer to let the web.config in peace. Especially, when issues can be worked out nicely programmatically.
In order to access the OnClick event of a control (or, any event you can’t access for that matter), you will need to modify your code to bind the event to an event receiver programmatically.
In effect, you will need to add (or, override) your Page_Init.
You need to:
1. override the Page_Init:
protected new void Page_Init(object sender, EventArgs e)
{
buttonControl.Click +=new EventHandler(buttonControl_Click);
}
2. Create the buttonControl_Click event receiver as you normally would:
protected void buttonControl_Click(object sender, EventArgs e)
{
//code for whatever happens when the button is clicked}
original solution found here.