Using SPMenuField in an SPGridView you can create drop down menu options for each item in the list – just like the ECB in SharePoint.
Sometime you may want to handle each menu item click as a server-side event, i.e. a postBack. I have seen a few different approaches for achieving this, but I think the cleanest and most intuitive is to raise server-side events against the parent SPGridView. I’ll describe this approach below.
The key is to use the ClientOnClickUsingPostBackEvent property of each MenuItemTemplate. The value of this property has the format [ID of control to receive event],[argument for the event]. So if the ID of your parent grid is grdItems, you should set the value of this property to “grdItems,[argument for the event]“.
[argument for the event] should have the format [command name]$[command argument].
Using this approach fires the OnRowCommand server-side event on the parent grid when the menu item is clicked. The [command name] and [command argument] is passed in to our event handler as the CommandName and CommandArgument properties of the event parameter respectively.
So in the code example below, the grid ID is grdItems, which has an event handler for the OnRowCommand, namely grdItems_RowCommand. The menu item “Refresh from Source” would fire the OnRowCommand event on the grid, passing the command name as “Refresh” and the command argument as “ItemKey” (for now the value ItemKey is hardcoded and will be the same for all items – we will fix this later).
<SharePoint:SPGridView ID="grdItems" AutoGenerateColumns="false" runat="server" OnRowCommand="grdItems_RowCommand"> <Columns> <SharePoint:SPMenuField HeaderText="Item Key" TextFields="Key" MenuTemplateId="grdItemsMenu" /> </Columns> </SharePoint:SPGridView> <SharePoint:MenuTemplate ID="grdItemsMenu" runat="server"> <SharePoint:MenuItemTemplate Text="Refresh from Source" ImageUrl="/_layouts/images/recurrence.gif" runat="server" ClientOnClickUsingPostBackEvent="grdItems,Refresh$ItemKey" /> </SharePoint:MenuTemplate>
The code behind would look like this:
protected void grdItems_RowCommand(object sender, GridViewCommandEventArgs e) { switch (e.CommandName.ToLower()) { case "refresh": HandleRefreshCommand((string)e.CommandArgument); break; } } private void HandleRefreshCommand(string itemKeyToRefresh) { //Do your stuff }
The code above pass in the hardcoded value “ItemKey” for all items. We need to change it so that the actual Key of the selected item is passed in. To do this, add the attribute TokenNameAndValueFields=”ItemKey=Key” to the SPMenuField’s markup. This tells the SPMenuField that the token %ItemKey% can be used in the MenuItemTemplate, and its value will be replaced with the data field Key. We also need to change the ClientOnClickUsingPostBackEvent attribute in the MenuItemTemplate’s markup to be “grdItems,Refresh$%ItemKey%“.
The markup for the grid should now be as follow:
<SharePoint:SPGridView ID="grdItems" AutoGenerateColumns="false" runat="server" OnRowCommand="grdItems_RowCommand"> <Columns> <SharePoint:SPMenuField HeaderText="Item Key" TextFields="Key" MenuTemplateId="grdItemsMenu" TokenNameAndValueFields="ItemKey=Key" /> </Columns> </SharePoint:SPGridView> <SharePoint:MenuTemplate ID="grdItemsMenu" runat="server"> <SharePoint:MenuItemTemplate Text="Refresh from Source" ImageUrl="/_layouts/images/recurrence.gif" runat="server" ClientOnClickUsingPostBackEvent="grdItems,Refresh$%ItemKey%" /> </SharePoint:MenuTemplate>
The other nice thing with this approach is you can actually make your menu item fires the grid’s OOTB events, e.g. OnRowDeleting, OnSorting, OnSelectedIndexChanging, etc. You do this by specifying a command name that is known to the GridView class (parent of SPGridView). For example, to trigger the OnRowDeleting event, use the command name Delete. Use ILSpy or dotPeek to check out the GridView.HandleEvent() method to see the known command names. Beware however, that some of the OOTB command names (e.g. Delete) expects the command argument to be the index of the item. If you use these, make sure you pass in the appropriate command argument.
Great!!!
Have not seen any post like this before (Maybe other found it.)
Again…. Great!!!!!!!!!!!!!
Great post! Thanks a lot, have been a lot of hours searching 🙂
Great!, thank you