Custom menus in RIA controls
Posted by - NA - on 23 December 2008 07:39 PM

In the 2.02 version in RIA controls you are now able to edit the context menu of the Grid and Chart cells.

You can do that by handling the OnShowContextMenu and OnContextMenuClick events of the RiaOLAPGrid or RiaOLAPChart controls.

The first one lets you edit contents of the default context menu (add your own menu items, and edit or even delete the existing ones).

A context menu item is represented by the TGenericMenuItem class. Below is the description of its basic properties:

Property Description
ActionType Set is compulsory!
For TGenericMenuItems, created in the OnShowContextMenu events handler, this property must be set into CustomAction (with the exception of menu items with children – in this case it should be Nothing).
Caption Set is compulsory!
A caption of the context menu item.
ChildItems The list of “children” menu items – in this case, the menu is multi-level.
HandlingType Set is compulsory!
The menu item click handling type: it can be handled on the server through a Callback-request (AJAX), Postback-request, or a client-side JScript function can be called (ClientOnly).
IsChecked True or False, depending of the required status of the “Checked” menu item.
MenuItemValue For the Callback and Postback handling types – the unique value, passed to the OnContextMenuClick event handler, that allows identifying which menu item was clicked.
ClientScript For the ClientOnly handling type – the JScript-function to be called.

The most complicated of them is the HandlingType property. The value of this property is selected in accordance with the following criteria:

  1. If a click on this menu item calls a client JScript-function, this item’s value is set to ClientOnly, and the calling code is written in the ClientScript property.
  2. If a click on this menu item is handled on the server, and nothing, except the Grid state, is changed, you should use the “Callback” type.
  3. If by a click on this menu item, the state of other objects on the web-page is changed, or files are passed to the client (or any other action incompatible with the asynchronous request takes place), you should use the “Postback” type.

In the example below, you can find realization of all three types of menu items:

// The OnShowContextMenu event handler
protected void TOLAPGrid1_OnShowContextMenu(object sender, ShowRIAContextMenuEventArgs e)
{
TGenericMenuItem mi = new TGenericMenuItem();
 
    // for custom context menu items ActionType property should be set to the CustomAction
    mi.ActionType = TGenericMenuActionType.CustomAction;
    mi.Caption = "Toggle Grid";
    // Since only the Grid contains are changed, it is possible to use the Callback-mode
    mi.HandlingType = TClientActionHandlingType.Callback;
    mi.IsChecked = false;
    mi.MenuItemValue = "Toggle";
 
    e.ContextMenu.Add(mi);
 
    mi = new TGenericMenuItem();
    // for custom context menu items ActionType property should be set to the CustomAction
    mi.ActionType = TGenericMenuActionType.CustomAction;
    mi.Caption = "Export to XLS";
    // Export operation. A file is passed to the client, hence the use of callback is impossible.
    // We'll use the postback instead.
    mi.HandlingType = TClientActionHandlingType.Postback;
    mi.IsChecked = false;
    mi.MenuItemValue = "Export";
 
    e.ContextMenu.Add(mi);
 
    mi = new TGenericMenuItem();
    // for custom context menu items ActionType property should be set to the CustomAction
    mi.ActionType = TGenericMenuActionType.CustomAction;
    mi.Caption = "Show alert";
    // Call of the JScript-function. So the handling type - ClientOnly.
    mi.HandlingType = TClientActionHandlingType.ClientOnly;
    mi.IsChecked = false;
    mi.ClientScript = "alert('The menu item is clicked');";
 
    e.ContextMenu.Add(mi)
 
}

// The OnContextMenuClick event handler
protected void TOLAPGrid1_OnContextMenuClick(object sender, ContextMenuClickArgs e)
{
    if (e.MenuItemValue == "Toggle")
        TOLAPGrid1.ToggleLayout();
 
    if (e.MenuItemValue == "Export")
{
        Response.ContentType = "APPLICATION/OCTET-STREAM";
        Response.AppendHeader("Content-Disposition", "Attachment; Filename=export.xls");
        TOLAPGrid1.Export.ExportTo(Page.Response.OutputStream, TConvertType.ctXLS);
        Response.End();
    }
}

For an end user the menu with the added items will look like this:

(326 vote(s))
This article was helpful
This article was not helpful

Help Desk Software by Kayako Resolve