I spend a fair amount of time before figuring out how to implement a Menu Item that would trigger a Post Back Event. So, thanks to Powlo's blog, SharePoint Users Group and the MSDN forum.
My overall goal was to implement a "Delete" MenuItemTemplate that triggers a Event on the server. Here are the steps for my implementation:
- Implement the IPostBackEventHandler and its RaisePostBackEvent method.
- Add the MenuItemTemplate as follow in the aspx file:
<SharePoint:MenuItemTemplate ID="DeletePortfolioMenuItem"
runat="server"
Text="Delete"
Description="Delete this Portfolio"
ImageUrl="/_layouts/images/delitem.gif"
Sequence="2"
ClientOnClickPostBackConfirmation="Are you sure you want to delete this item?"
ClientOnClickUsingPostBackEvent="__page,%Name%" />
Or in the codebehind by setting the ClientOnClickPostBackConfirmation and ClientOnClickUsingPostBackEvent in the CreateChildControls method:
protected override void CreateChildControls()
{
base.CreateChildControls();
DeletePortfolioMenuItem.ClientOnClickPostBackConfirmation = "Are you sure you want to delete this item?";
DeletePortfolioMenuItem.ClientOnClickUsingPostBackEvent = "__page,%Name%";
}
The %Name% parameter is used here as the TokenNameAndValueFields in the SPMenuField of the SPGridView. Its value is passed to the Event Argument of the RaisePostBackEvent method.
Note: The functionality can also be implemented with the ClientOnClickScript as follow:
DeletePortfolioMenuItem.ClientOnClickScript = "if (confirm('Are you sure you want to delete this item?')) __doPostBack('" + this.UniqueID + "','%Name%')";