Working with CommandManager
Introduction
CommandManager is a specific class for handling actions in Commerce Manager.
The following command types are available:
- Navigate – navigation to another page
- OpenWindow – open popup in new window
- OpenFrameModalPopup – open modal popup (IFrame) in current window
- ServerAction – server action (POST) with calling of special server handler
- ClientAction – client action with calling of predefined Javascript
The command types are described in more detail below.
Commands defined in XML files
Commends are defined in the View/Commands section of an XML file.
The command description looks like this:
<Command id="Command_Name1">
<CommandType>Navigate</CommandType>
...
</Command>
The example below shows how to bind a command to a toolbar button simply by adding the commandName attribute with the command identifier to Button.
<Button id="Button1" text="button text" commandName="Command_Name1"></Button>
The command parameters are described in more details below.
Navigate
The navigate-type command can contain the following tags:
- Url (string) – URL to page for navigation.
- Target (string) – navigate to named frame.
- EnableHandler – server handler in the format "className, assemblyName" defining accessibility of the command.
The server handler must implement the ICommandEnableHandler interface:
public class CanAdminHandler : ICommandEnableHandler
{
public bool IsEnable(object Sender, object Element)
{
bool retval = false;
return retval;
}
}
Example:
<Command id="MC_ListApp_ManageList">
<CommandType>Navigate</CommandType>
<Url>~/Apps/ListApp/ListInfoView.aspx?class={QueryString:ViewName}</Url>
</Command>
You can use the following templates as dynamic parameters for the Url parameter:
- {QueryString:KeyName} – get value for "KeyName" parameter from QueryString for current page.
- {HttpContext:KeyName} – get value for "KeyName" from HttpContext.
- {Session:KeyName} – get value from "KeyName" from ASP.NET Session.
- {DataContext:KeyName} – get value from "KeyName" from DataContext.
- {Security:KeyName} – get value from security context. KeyName can get the following values:
- CurrentUser (CurrentUserId) – ID of current user.
- {DateTime:KeyName} – get date value. Key name can get following values for date:
- Today (TodayStart) – starting of current day
- ThisWeek (ThisWeekyStart) – starting of current week
- ThisMonth (ThisMonthStart) – starting of current month
- ThisYear (ThisYearStart) – starting of current year
- Yesterday (YesterdayStart) – starting of previous day
- LastWeek (LastWeekStart) – starting of previous week
- LastMonth (LastMonthStart) – starting of previous month
- LastYear (LastYearStart) – starting of previous year
- TodayEnd – finishing for current day
- ThisWeekEnd - finishing of current week
- ThisMonthEnd – finishing of current month
- ThisYearEnd – finishing of current year
- YesterdayEnd – finishing of previous day
- LastWeekEnd – finishing of previous week
- LastMonthEnd - finishing of previous month
- LastYearEnd – finishing of previous year
- [ClientParam:ParameterName] – gets parameter name from CommandParameters collection, for example [ClientParam:PrimaryKeyId].
OpenWindow
The OpenWindow-type command can contain the following tags:
- Url (string) – url to page for opening in pop-up window. You can use the same templates as in the Navigate-type command.
- Width (int) – width in pixels for window(default, 640).
- Height (int) – height in pixels for window(default, 480).
- Left (int) – left position for window (show in center by default).
- Top (int) – top position for window(show in center by default).
- Scroll (True/False) – allow scroll bar for window or not.
- Resize (True/False) – allow resize for window or not.
- RefreshMethod (string) – javascript function name with one parameter, which will be called after window close.
- UpdatePanelIds (string) – list of UpdatePanels IDs, which necessary to update after window close.
- EnableHandler (string) – server handler in format "className, assemblyName" which define accessibility of the command.
In WEB we do not have a standard use-case for monitoring closed pop-up windows, so we need to make some additional actions. To update the parent window, we need to run the following code:
CommandManager.RegisterRefreshParentWindowScript(this.Page, param);
Here param – string parameter, which will be sent to the Javascript function; if you do not need any parameters for the client side, you can send String.Empty.
Use the following code to close the current pop-up windows and refresh parent windows:
CommandManager.RegisterCloseOpenedWindowScript(this.Page, param);
For updating UpdatePanels in the parent window (which are defined in the XML description), run the following code:
CommandParameters cp = new CommandParameters(commandId);
CommandManager.RegisterRefreshParentWindowScript(this.Page, cp.ToString());
or
string command = String.Empty;
if (!String.IsNullOrEmpty(CommandName))
{
CommandParameters cp = new CommandParameters(CommandName);
command = cp.ToString();
}
Here commandId – command name, which is mapped from <Commandid=""> in the XML.
Example:
<Command id="MC_TimeTracking_MultipleAdd">
<CommandType>OpenWindow</CommandType>
<Url>~/Apps/MultipleAdd.aspx?ViewName={QueryString:ViewName}</Url>
<Width>500</Width>
<Height>375</Height>
<Resize>False</Resize>
<Scroll>False</Scroll>
<UpdatePanelIds>UpdatePanel1,UpdatePanel2</UpdatePanelIds>
</Command>
CommandParameters cp = new CommandParameters("MC_TimeTracking_MultipleAdd");
CommandManager.RegisterRefreshParentWindowScript(this.Page, cp.ToString());
OpenFrameModalPopup
The OpenFrameModalPopup-type of command can contain these tags:
- Url (string) – URL to page for opening in pop-up window. You can use the same templates as in the Navigate-type command.
- Width (int) – width in pixels for window (default, 640).
- Height (int) – height in pixels for window (default, 480).
- Left (int) – left position for window (displayed in center by default).
- Top (int) – top position for window (displayed in center by default).
- PopupTitle (string) – pop-up window title.
- Drag (True/False) – allow to drag window or not (default False).
- UpdatePanelIds (string) – list of UpdatePanels IDs, which is necessary to update after the window closes.
- EnableHandler (string) – server handler in format "className, assemblyName", which defines accessibility of the command.
- AutoHeightResize (True/False) – when True, the pop-up dialog will adjust its height if it cannot be displayed in the browser.
Example:
<Command id="MC_ListApp_AddMetaField">
<CommandType>OpenModalPopup</CommandType>
<Url>~/Apps/MetaFieldListEdit.ascx</Url>
<Width>600</Width>
<Height>440</Height>
<Left>20</Left>
<Top>20</Top>
<PopupTitle>{IbnFramework.ListInfo:tAddField}</PopupTitle>
</Command>
The .ascx-control, which should be displayed in dialog, must implement the IModalPopupControl interface:
public partial class QuickAddControl : System.Web.UI.UserControl, IModalPopupControl
{
#region IModalPopupControl Members
public void BindData(string p)
{
}
public string ContainerId
{
get
{
if (ViewState["ContainerId"] != null)
return ViewState["ContainerId"].ToString();
else
return String.Empty;
}
set
{
ViewState["ContainerId"] = value;
}
}
public string ScriptHidePopup
{
get
{
if (ViewState["ScriptHidePopup"] != null)
return ViewState["ScriptHidePopup"].ToString();
else
return String.Empty;
}
set
{
ViewState["ScriptHidePopup"] = value;
}
}
#endregion
}
For updating the parent window and closing the ModalPopup, run the following code:
CommandParameters cp = new CommandParameters(commandId);
CommandManager.RegisterCloseOpenedFrameScript(this.Page, cp.ToString());
If the parent window contains UpdatePanels, and we need to run a full update for the window when the button id="Btn1" is pressed, we can run the next code in Page_Load for the .ascx control:
ScriptManager.GetCurrent(this.Page).RegisterPostBackControl(Btn1);
For closing ModalPopup:
CancelButton.OnClientClick =
CommandManager.GetCloseOpenedFrameScript(
this.Page, String.Empty, false, true);
If you need to run another command from ModalPopup:
CommandManager cm = CommandManager.GetCurrent(this.Page);
Dictionary<string, string> prms = new Dictionary<string, string>();
string command = cm.AddCommand(className, viewName, placeName, "MC_TimeTracking_MultipleAdd", prms);
MultipleAddLink.NavigateUrl = String.Format(
CultureInfo.InvariantCulture,
"javascript:{0};{1}", ScriptHidePopup, command);
Example: UI for IFrame ModalPopup:
These are the steps to create an example of a dialog for a custom page "MyPage.aspx".
- Create an .ascx control implementing the IModalPopupControl interface, and name it MyControl.ascx.
- Create an XML file with the name "MyPage.xml".
<?xml version="1.0" encoding="utf-8" ?>
<View xmlns="http://schemas.mediachase.com/ecf/view">
<ListViewUI>
<Commands>
<add>
<Command id="MyCommand">
<CommandType>OpenModalPopup</CommandType>
<Url>~/Apps/MyControl.ascx</Url>
<Width>300</Width>
<Height>200</Height>
<Left>100</Left>
<Top>100</Top>
<Drag>True</Drag>
<PopupTitle>Some Title</PopupTitle>
</Command>
</add>
</Commands>
</ListViewUI>
</View>
Run the following code for MyPage in order to display the dialog:
CommandManager cm = CommandManager.GetCurrent(this.Page);
CommandParameters cp = new CommandParameters("MyCommand");
string command = cm.AddCommand("MyPage ", "", "", cp);
After running the AddCommand method, the variable command will contain Javasctipt code. If you add the "javascript:" prefix, you can then use the NavigateUrl property for the HyperLink control .
ServerAction
The ServerAction-type command can contain the following tags:
- ConfirmationText (string) – text that show in confirm-dialog, if this parameter is not defined then confirm-dialog will not be displayed.
- Handler (string) – server handler for command in format "className, assemblyName".
- UpdatePanelIds (string) – list of UpdatePanels IDs, which necessary to update after window close.
- EnableHandler (string) – server handler in format "className, assemblyName" which define accessibility of the command.
Example:
<Command id="MC_ListApp_Selected_Delete">
<CommandType>ServerAction</CommandType>
<ConfirmationText>{IbnFramework.Common:DeleteConfirm}</ConfirmationText>
<Handler>Mediachase.Ibn.DeleteSelectedItemsHandler, Mediachase.UI.Web</Handler>
</Command>
The server handler must implement the ICommand interface. Below is an example of the server handler, with actions for selected elements in the grid:
public class DeleteHandler : ICommand
{
public void Invoke(object Sender, object Element)
{
if (Element is CommandParameters)
{
CommandParameters cp = (CommandParameters)Element;
string[] selectedElements = MetaGrid.GetCheckedCollection(((CommandManager)Sender).Page, cp.CommandArguments["GridId"]);
foreach (string elem in selectedElements)
{
string id = elem.Split(new string[] { "::" }, StringSplitOptions.RemoveEmptyEntries)[0];
// Here some actions for elements
// ...
}
}
}
}
Example showing how to get the primaryKeyId for a grid record:
CommandParameters cp = (CommandParameters)Element;
if (cp.CommandArguments["primaryKeyId"] == null)
throw new ArgumentException();
PrimaryKeyId pk = PrimaryKeyId.Parse(cp.CommandArguments["primaryKeyId"]);
ClientAction
The ClientAction-type command can contain the following tags:
- ClientScript – string containing Javascript, use a template like{TemplateType:KeyName} (see the Navigate section for description).
- EnableHandler (string) – server handler in the format "className, assemblyName" defining accessibility of the command.
Do you find this information helpful? Please log in to provide feedback.
Last updated: May 28, 2015