OnClientContextMenuItemClicked
The contextMenuItemClicked event is fired when the user clicks on a ContextMenu Item.
The event handler receives two parameters:
-
The instance of the clicked RadSplitButton control
-
An eventArgs parameter of type Telerik.Web.UI.RadSplitButtonContextMenuItemClickedEventArgs, containing the following properties and methods:
-
get_item() - returns the clicked Menu Item
-
get_newButtonText() - gets the
Text
that is about to be set as RadSplitButton's Text. By default returnsText
of the clicked MenuItem. -
set_newButtonText() - sets the
Text
that is about to be set as RadSplitButton's Text -
get_newCommandName() - gets the
CommandName
that is about to be set as RadSplitButton's Text. By default returnsValue
of the clicked MenuItem. -
set_newCommandName() - sets the
CommandName
that is about to be set as RadSplitButton's Text -
get_newCommandArgument() - gets the
CommandArgument
that is about to be set as RadSplitButton's Text. By default returns an empty String. -
set_newCommandArgument() - sets the
CommandArgument
that is about to be set as RadSplitButton's Text
-
This event provides to the user the opportunity to have full control over the
Text
,CommandName
andCommandArgument
that will be processed to the Server.
Example:
<script>
function OnClientContextMenuItemClicked(sender, args) {
args.set_newButtonText("New Text");
args.set_newCommandArgument("New CommandArgs");
args.set_newCommandName("New CommandName");
}
</script>
<telerik:RadSplitButton runat="server" AutoPostBack="true" ID="RadSplitButton1" Text="Paste" CommandName="Paste"
OnClientContextMenuItemClicked="OnClientContextMenuItemClicked" OnCommand="RadSplitButton1_Command">
<ContextMenu>
<Items>
<telerik:RadMenuItem Text="Paste" Value="Paste"></telerik:RadMenuItem>
<telerik:RadMenuItem Text="Paste plain" Value="PastePlain"></telerik:RadMenuItem>
<telerik:RadMenuItem Text="Paste as HTML" Value="PasteHtml"></telerik:RadMenuItem>
</Items>
</ContextMenu>
</telerik:RadSplitButton>
<br />
<asp:Label Text="Label1" ID="Label1" runat="server" />
protected void RadSplitButton1_Command(object sender, CommandEventArgs e)
{
var button = sender as RadSplitButton;
var commandName = e.CommandName;
var commandArgument = e.CommandArgument;
Label1.Text = String.Format("<br />CommandName: <b>{0}</b>;<br /> CommandArgument:<b>{1}</b>", commandName, commandArgument);
}
Set different CommandArgument for each Menu item
The embedded ContextMenu in RadSplitButton can handle custom attributes as described in RadMenu Custom Attributes. With the help of the OnClientContextMenuItemClicked event we can set different CommandName and CommandArgument upon clicking each MenuItem:
<telerik:RadSplitButton runat="server" ID="RadSplitButton1" Text="Paste"
CommandName="Paste" CommandArgument="Default Argument"
OnClientContextMenuItemClicked="OnClientContextMenuItemClicked"
OnCommand="RadSplitButton1_Command" Skin="Silk">
<ContextMenu>
<Items>
<telerik:RadMenuItem Text="Paste" CommandName="Paste" CommandArgument="Custom Argument 1"></telerik:RadMenuItem>
<telerik:RadMenuItem Text="Paste as Plain Text" CommandName="PastePlainText" CommandArgument="Custom Argument 2"></telerik:RadMenuItem>
<telerik:RadMenuItem Text="Paste as HTML" CommandName="PasteHtml" CommandArgument="Custom Argument 3"></telerik:RadMenuItem>
</Items>
</ContextMenu>
</telerik:RadSplitButton>
<br />
<asp:Label Text="" ID="Label1" runat="server" />
function OnClientContextMenuItemClicked(sender, args) {
var item = args.get_item();
//get the custom attributes (CommandName and CommandArgument) set to the items
var itemCommandName = item.get_attributes().getAttribute("CommandName");
var itemCommandArg = item.get_attributes().getAttribute("CommandArgument");
args.set_newCommandName(itemCommandName);
args.set_newCommandArgument(itemCommandArg);
}
protected void RadSplitButton1_Command(object sender, CommandEventArgs e)
{
var commandName = e.CommandName;
var commandArgument = e.CommandArgument;
Label1.Text += String.Format("<br />CommandName: <b>{0}</b>;<br />CommandArgument: <b>{1}</b>", commandName, commandArgument);
}