Search code examples
c#dotnetnukedotnetnuke-module

DotNetNuke ModuleAction server-side processing


In DNN it is possible to add a ModuleAction menu item. According this article on the DNN site it is even possible to do some additional processing on the server-side. After converting the code to C#, the ActionHandler is never called.

This is my code:

public ModuleActionCollection ModuleActions
{
    get
    {
        ModuleActionCollection Actions = new ModuleActionCollection();
        ModuleAction urlEventAction = new ModuleAction(ModuleContext.GetNextActionID());
        urlEventAction.Title = "Action Event Example";
        urlEventAction.CommandName = "redirect";
        urlEventAction.CommandArgument = "cancel";
        urlEventAction.Url = "http://dotnetnuke.com";
        urlEventAction.UseActionEvent = true;
        urlEventAction.Secure = DotNetNuke.Security.SecurityAccessLevel.Admin;
        Actions.Add(urlEventAction);
        return Actions;
    }
}

private void MyActions_Click(object sender, DotNetNuke.Entities.Modules.Actions.ActionEventArgs e)
{
    DotNetNuke.UI.Skins.Skin.AddModuleMessage(this, string.Format(Localization.GetString("ClickMessage", LocalResourceFile), e.Action.CommandName), ModuleMessage.ModuleMessageType.BlueInfo);

    switch (e.Action.CommandName.ToUpper())
    {
        case "REDIRECT":
            if (e.Action.CommandArgument.ToUpper() != "CANCEL")
            {
                Response.Redirect(e.Action.Url);
            }
            else
            {
                DotNetNuke.UI.Skins.Skin.AddModuleMessage(this, "Canceled the Redirect", ModuleMessage.ModuleMessageType.YellowWarning);
            }
            break;
    }
}

And in the page init I attach the event handler:

AddActionHandler(new ActionEventHandler(MyActions_Click));

I also tried attaching in the page load as done by DNN source itself. The menu item is shown and the redirect to http://dotnetnuke.com is executed. But my breakpoint in MyActions_Click is never hit.

What am I doing wrong?

I'm running in DotNetNuke 7.1 with module reference to DNN 6.2.


Solution

  • My solution with IPostBackEventHandler instead of the DNN way (until someone corrects me):

    public ModuleActionCollection ModuleActions
    {
        get
        {
            ModuleActionCollection Actions = new ModuleActionCollection();
            Actions.Add(ModuleContext.GetNextActionID(),
                        "Bla",
                        "",
                        "",
                        "",
                        "javascript:" + Page.ClientScript.GetPostBackEventReference(this, "ARGUMENT"),
                        Page.ClientScript.GetPostBackEventReference(this, "ARGUMENT"),
                        false,
                        DotNetNuke.Security.SecurityAccessLevel.Edit,
                        true,
                        false);
            return Actions;
        }
    }
    
    public void RaisePostBackEvent(String eventArgument)
    {
        if (eventArgument.ToUpper() == "ARGUMENT")
        {
            ...
    
            Globals.Redirect(HttpContext.Current.Request.RawUrl, false);
        }
    }
    

    And don't forget to add IPostBackEventHandler to your page class name.

    Namespace: using System.Web.UI;