An Avanade Blogging Community

Welcome to An Avanade Blogging Community Sign in | Join | Help
in Search

Johan's Avanade Blog

MenuItemTemplate and ClientOnClickUsingPostBackEvent

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:

  1. Implement the IPostBackEventHandler and its RaisePostBackEvent method.
  2. 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%')";

Published Wednesday, June 11, 2008 6:01 AM by johanr

Comments

 

johanr said:

"Menu Item Post Back and Single Quotes"
A little note regarding the Menu Items:  I just wasted 3 hours before figuring out that my %Name% field value contained a single quote ' that breaks the process of the Post Back when you click on one Menu Item !!!
June 12, 2008 1:24 PM
Anonymous comments are disabled

This Blog

Post Calendar

<June 2008>
SuMoTuWeThFrSa
25262728293031
1234567
891011121314
15161718192021
22232425262728
293012345

Syndication