A control with the specified ID could not be found within the scope of the current naming container : SPGridView – SPMenuField

Monday, July 27, 2009 5:06 PM

Lately I was working on the SPGridView provided by SharePoint.One of the requirement I had was to have a column in SPGridView as a Menu one that is similar to ECB of the List items, it  was straight forward, refer to the Paul article for the example, though the example was targeted towards building Webpart and controls were created programmatically, converting it to work with UserControl is not a big deal, Visual Studio Intellisense will help you and Property names are pretty straight forward.

While I was adding a SPMenuField there were couple of Properties of SPMenuField that drawn my attention.

To help you understand the issue better I have given the screen shot of my code {bear with me As I don't have a tool that will format the code with color, I have given it as a screenshot,so that it will be easy to understand}

HiddenMenuItemIdsFields

From the name of the property it is clear that it will help us to define which menu item that needs to be Hidden. So I jumped in to and gave the Id of one of the MenuItemTemplate I wanted to disable in my case MenuItemTemplate1, result System.InvalidOperationException: Field not found: MenuItemTemplate1

                SPMenuTemplate 

                From the exception I got, it was pretty clear that it is expecting a Field from the DataSource, List in our case. So Next thing I tried was to put a Field in the List, instead of the Id of the MenuItemTemplate, and now I got System.ArgumentException: A control with the specified ID could not be found within the scope of the current naming container: XYZ, this time It gave me different exception. I ran through net and didn't find any thing that gave a right approach. Finally after going through the Reflector and  with the Value of XYZ in the Exception Message I got the actual meaning of it.

                It means that HiddenMenuItemIdsFields expects a field from the list, whose  value corresponds to the MenuItemTemplate that needs to be hidden.

                And if this confuses you refer the below diagram.

                 

                SPMenuTemplate1

                 

                I have two column in the List “Title,MenuItemValueToDisable” the trick is in MenuItemValueToDisable field. We need to set the value For HiddenMenuItemIdsField=”MenuItemValueToDisable” and the value in the Field should have an Id of the MenuItemTemplate. As in the example I have two value respective to the each of the MenuItemTemplate I have defined so for the first row I will not have the “Details” menu option and for the second More Details menu option will be disabled.

                Same rule applies for the following fields as well

            • DisabledMenuItemIdsFields - (Not only the Single field you can give more than one fields separated by comma)
            • CheckedMenuItemIdsFields (I didn't know how to use it – If someone has used it please let me know I can I utilize it)(Not only the Single field you can give more than one fields separated by comma)
            • MenuTemplateIdField  - One one Field is accepted here.
            The fields you have specified for any of the above property should have a valid value, in case if it has value.If it is empty there will not be any error, control will render all the menus.
 
Please add 7 and 5 and type the answer here: