How to: Troubleshoot “The event handler ‘OnClick’ is not allowed in this page”

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.

MGR: the Intelogist

About MGR: the Intelogist

SharePoint Server developer, turned Sitefinity developer, turned Angular developer, turned SharePoint Online consultant, turned Unily consultant, turned O365 consultant... Never a dull moment!

Leave a Reply

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

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> 

This site uses Akismet to reduce spam. Learn how your comment data is processed.