One of my recent projects was to develop a new menu system for our SharePoint portal to replace our old menu system (which was a custom menu as well). I decided to create an ASP.Net server control to do this. I created my control, signed it, deployed it to the GAC, added it to the safe control list, and then our web master put it into production after we did some basic testing. So far, so good.
However, today our web guy hit a snag when he put the menu on the SharePoint administrator pages (located in the layouts/1033 folder). When the menu was put into these pages the menu would display but the scripting didn't work. I looked into the problem and quickly realized that my client side script was not being rendered into the page, basically my RegisterClientScriptBlock calls were being ignored (or so it seemed).
After some doing some googling and running some tests I finally realized that the SharePoint admin pages have no form tags (mark this down as YASPQ). So the problem I was running into was that since there were no form tags, the client side script blocks were not rendered as decribed here:
Server controls that post back or use client-side script will not work if they are not enclosed in the HtmlForm server control (
The method referred to here is Page.VerifyRenderingInServerForm. I could call this method in the Render method of my menu control, but this just throws an HttpException (which in this case is clearly not beneficial). Instead of allowing the exception to bubble up I simply trapped for the exception and if it was thrown I added an HtmlForm control to my menu's controls collection and then rendered it after my menu was built.
This allowed the client script to be rendered even if the form tag was missing. So if you're developing a control and run into this problem this is a quick work-a-round. If your control has other controls that need to be posted back you could add them as child controls to the HtmlForm control and it should work as expected.