Wednesday, January 11, 2006

Validation Controls and Webparts

I'm currently working on a project that has a SharePoint webpart with an input form on it. The form has several ASP.NET validator controls on it. The problem begins when I want to modify a webpart's property (not just my webpart) with my webpart on my page. None of the submit buttons work, like 'Ok', 'Apply', 'Cancel', 'Upload', etc. I googled for awhile trying to find a nice easy way to detect that the page was in edit mode but I could not. I found a way to check if my webpart was in edit mode but the problem still would exist if I was editing another webpart or wanted to add another webpart to the page.


So I came up with a custom solution. I wrote a loop routine for all the controls loaded on the Page object and outputted those control IDs. After reviewing the controls loaded on a normal view and edit view of a page, I found that the control named MSOWebPartPage_MenuPostback is loaded to the page only if we are in edit mode. So I wrote a method that recursively searches all the controls loaded to the Page object for MSOWebPartPage_MenuPostback. If the control is found, then I set the validator control's EnableClientScript property to false, otherwise it is set to true. So now the clientside validation is active only when we are not in edit mode. If you do use this technique, you will have to always check that your validator controls validate correctly on the server-side.


Turns out that the above idea wasn't the greatest. Apparently anytime a SharePoint page does a postback, the MSOWebPartpage_MenuPostback control is added. So it does not do an effective job detecting if the page is in edit mode. I however did find the built-in webpart property BrowserDesignMode. This property successfully detects if the page is in edit mode (Modifying a webpart, adding/importing webparts, etc). The rest of the solution works fine with the BrowserDesignMode property replacing the search for the MSOWebPartpage_MenuPostback control.

No comments: