Frequently, the Action items you add to the Action menu of a module redirect to another page or another module to implement the functionality. DNN core has several of these examples (trivial ones being the module settings and documentation actions). However, when I wanted to have an Action that simply posted back to the same module I was lost.
As it turns out, this is trivial to implement (as you'd hope and expect), but to figure out how it worked I had to spend an hour or so digging around the core code. This quick article is so (hopefully) you won't have to repeat that.
Step 1 - Add Action Item
The first step, obviously, is to declare the Action item you'd like added to the menu. The key difference is that we're not going to supply a target URL -- just a command name:
With MyBase.Actions
.Add(GetNextActionID, "My Menu Option", CmdName:="MenuOptionCommand", Secure:=SecurityAccessLevel.Admin)
End With
Step 2 - Wire The Event Handler
This is the part that took me awhile to figure out. The event for the action item is raised through the Skin object. When the module is loaded, we need to register an event handler to catch the selection of our Action menu item:
With Skin.GetParentSkin(Me)
.RegisterModuleActionEvent(ModuleId, AddressOf ModuleAction_Click)
End With
This delegate needs to point to a method with the appropriate signature of course -- in this case, it's just the general Event handler signature:
Public Sub ModuleAction_Click(ByVal sender As Object, ByVal e As ActionEventArgs)
End Sub
Step 3 - Complete Your Event Handler
Now that you've declared and registered your event handler, and declared your menu item, you can fill in the handling code for the event -- this obviously needs to take into account the command argument from Step 1, in the event that you're handling multiple menu items:
Public Sub ModuleAction_Click(ByVal sender As Object, ByVal e As ActionEventArgs)
Select Case e.Action.CommandName.ToLower
Case "menuoptioncommand"
PerformActionForCommand()
End Select
End Sub
Notes
The Actions.Add() method also takes an argument indicating whether or not the Action event should even fire (Optional UseActionEvent As Boolean = False) -- be aware that this event always fires if no url is supplied for the menu item.
That's it! As I mentioned -- trivial, but far from drop-dead easy if you're not sure where to catch the events.