RadEditor Context Menu shows name instead of text when added in code behind

0 Answers 76 Views
Editor
Jamex
Top achievements
Rank 1
Jamex asked on 01 Sep 2023, 03:25 AM

I'm using the following code to create multi-level context menus in a RadEditor control, but I can't get the Text property of the tool to display instead of the Name property. 

 


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="RadEditorForm.aspx.cs" Inherits="RadEditorForm" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <telerik:RadStyleSheetManager id="RadStyleSheetManager1" runat="server" />
</head>
<body>
    <form id="form1" runat="server">
    <telerik:RadScriptManager ID="RadScriptManager1" runat="server">
        <Scripts>
            <asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.Core.js" />
            <asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.jQuery.js" />
            <asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.jQueryInclude.js" />
        </Scripts>
    </telerik:RadScriptManager>
    <script type="text/javascript">
        //Put your JavaScript code here.
    </script>
    <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
    </telerik:RadAjaxManager>
    <div>
        <telerik:RadEditor ID="RadEditor1" runat="server" ToolsFile="RadEditorForm_ToolsFile.xml">
            <ContextMenus>
                <telerik:EditorContextMenu TagName="P">
                    <telerik:EditorTool Name="JustifyLeft"></telerik:EditorTool>
                    <telerik:EditorTool Name="JustifyCenter"></telerik:EditorTool>
                    <telerik:EditorTool Name="JustifyRight"></telerik:EditorTool>
                    <telerik:EditorTool Name="JustifyFull"></telerik:EditorTool>
                </telerik:EditorContextMenu>
                <telerik:EditorContextMenu TagName="BODY">                                    
                    <telerik:EditorTool Name="JustifyLeft"></telerik:EditorTool>
                    <telerik:EditorTool Name="JustifyCenter"></telerik:EditorTool>
                    <telerik:EditorTool Name="JustifyRight"></telerik:EditorTool>
                    <telerik:EditorTool Name="JustifyFull"></telerik:EditorTool>                                                        
                </telerik:EditorContextMenu>
            </ContextMenus>
        </telerik:RadEditor>
    </div>
    </form>
</body>
</html>


using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Configuration;
using System.Web.Security;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Telerik.Web.UI;

public partial class RadEditorForm : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        EditorContextMenuTool dataMenu = new EditorContextMenuTool("Data");
        dataMenu.Text = "Data X";
        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        for (int l1 = 0;l1 < 5;l1++)
        {
            EditorContextMenuTool subDataMenu = new EditorContextMenuTool();
            subDataMenu.Name = string.Concat("L",l1,"_Menu");
            subDataMenu.Text = string.Concat("Level 1 Menu Item ",l1);
            subDataMenu.ShowText = true;
            subDataMenu.ShowIcon = false;            
            for (int l2 = 0; l2 < 5; l2++)
            {
                EditorTool tool = new EditorTool();
                tool.Name = string.Concat("L",l1,"_Menu_",l2);
                tool.Text = string.Concat("Level ",l1,"-Menu Item ",l2);
                tool.ShowText = true;
                tool.ShowIcon = false;
                subDataMenu.Tools.Add(tool);
                sb.Append("Telerik.Web.UI.Editor.CommandList[\"" + tool.Name + "\"] = function (commandName, editor, oTool) {debugger;editor.pasteHtml(\"<var>" + tool.Text + "</var>\");}");
                sb.AppendLine();
            }
            dataMenu.Tools.Add(subDataMenu);
        }        
        RadEditor1.ContextMenus.FindByTagName("P").Tools.Add(dataMenu);
        RadEditor1.ContextMenus.FindByTagName("BODY").Tools.Add(dataMenu);
        ScriptManager.RegisterStartupScript(Page, Page.GetType(), "contextMenuHandlerScript", sb.ToString(), true);
    }
}

Rumen
Telerik team
commented on 01 Sep 2023, 08:20 AM

Hi James,

I verified that the Text property of the context menu item does not function when the RadMenu content menu is used (RadEditor1.UseRadContextMenu = true;) instead of the built-in one.

The solution is to modify the text of the context menu item on the client when RadEditor loads as shown below:

ASPX

        <script>
            function OnClientLoad(editor, args) {
                var contextMenu = editor.get_radContextMenu(); //get a reference to the context menu
                contextMenu.findItemByText("Data").set_text("Data X"); //find the item by text and set its new text
                contextMenu.findItemByText("L0_Menu").set_text("Level 1 Menu Item");
            }
        </script>
        <telerik:RadEditor ID="RadEditor1" runat="server" OnClientLoad="OnClientLoad">
            <ContextMenus>
                <telerik:EditorContextMenu TagName="P">
                    <telerik:EditorTool Name="JustifyLeft"></telerik:EditorTool>
                    <telerik:EditorTool Name="JustifyCenter"></telerik:EditorTool>
                    <telerik:EditorTool Name="JustifyRight"></telerik:EditorTool>
                    <telerik:EditorTool Name="JustifyFull"></telerik:EditorTool>
                </telerik:EditorContextMenu>
                <telerik:EditorContextMenu TagName="BODY">
                    <telerik:EditorTool Name="JustifyLeft"></telerik:EditorTool>
                    <telerik:EditorTool Name="JustifyCenter"></telerik:EditorTool>
                    <telerik:EditorTool Name="JustifyRight"></telerik:EditorTool>
                    <telerik:EditorTool Name="JustifyFull"></telerik:EditorTool>
                </telerik:EditorContextMenu>
            </ContextMenus>
        </telerik:RadEditor>

 

ASPX.CS

    protected void Page_Load(object sender, EventArgs e)
    {
        EditorContextMenuTool dataMenu = new EditorContextMenuTool("Data");
        dataMenu.Text = "Data X";
        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        for (int l1 = 0; l1 < 5; l1++)
        {
            EditorContextMenuTool subDataMenu = new EditorContextMenuTool();
            subDataMenu.Name = string.Concat("L", l1, "_Menu");
            subDataMenu.Text = string.Concat("Level 1 Menu Item ", l1);
            subDataMenu.ShowText = true;
            subDataMenu.ShowIcon = false;
            for (int l2 = 0; l2 < 5; l2++)
            {
                EditorTool tool = new EditorTool();
                tool.Name = string.Concat("L", l1, "_Menu_", l2);
                tool.Text = string.Concat("Level ", l1, "-Menu Item ", l2);
                tool.ShowText = true;
                tool.ShowIcon = false;
                subDataMenu.Tools.Add(tool);
                sb.Append("Telerik.Web.UI.Editor.CommandList[\"" + tool.Name + "\"] = function (commandName, editor, oTool) {debugger;editor.pasteHtml(\"<var>" + tool.Text + "</var>\");}");
                sb.AppendLine();
            }
            dataMenu.Tools.Add(subDataMenu);
        }
        RadEditor1.ContextMenus.FindByTagName("P").Tools.Add(dataMenu);
        RadEditor1.ContextMenus.FindByTagName("BODY").Tools.Add(dataMenu);
        ScriptManager.RegisterStartupScript(Page, Page.GetType(), "contextMenuHandlerScript", sb.ToString(), true);
    }

No answers yet. Maybe you can help?

Tags
Editor
Asked by
Jamex
Top achievements
Rank 1
Share this question
or