This is a migrated thread and some comments may be shown as answers.

write a RadContextMenu control error

1 Answer 48 Views
Menu
This is a migrated thread and some comments may be shown as answers.
tother
Top achievements
Rank 1
tother asked on 09 Apr 2010, 12:46 PM
Hi!

I'm trying to write a context menu control,for the reason to add context menu item dynamically!I wrote a web form page to invoke the control radcontextmenu.but when i click the href,I get a error:"Microsoft JScript runtime error: Object doesn't support this property or method"
web form page:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm4.aspx.cs" Inherits="WebApplication1.WebForm4" %> 
 
<%@ Register TagPrefix="CM" TagName="contextmenu" Src="~/contextmenu.ascx" %> 
<%@ Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %> 
<!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>  
</head> 
<body> 
      
    <form id="form1" runat="server">  
    <script type="text/javascript">  
    //<![CDATA[
    function popupMenu(item, e) {
        window.<%=contex1.ClientID%>.ShowContextMenu(item, e);
    }
    //]]> 
    </script> 
    <telerik:RadScriptManager ID="ScriptManager1" runat="server" /> 
    <div> 
        <CM:contextmenu ID="contex1" runat="server" /> 
        <onclick="popupMenu(this, event)">Click here!</a> 
    </div> 
    </form> 
 
</body> 
</html> 
 
web control form(contextmenu.ascx)
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="contextmenu.ascx.cs" Inherits="WebApplication1.contextmenu" %> 
<%@ Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %> 
<Telerik:RadContextMenu id="RadContextMenu1" 
                runat="server" > 
    <Items> 
        <Telerik:RadMenuItem Text="Trees" /> 
        <Telerik:RadMenuItem Text="Sunset" /> 
        <Telerik:RadMenuItem Text="Mountains" /> 
    </Items> 
</Telerik:RadContextMenu> 
web control form cs(contextmenu.ascx.cs)
using System;  
using System.Collections.Generic;  
using System.Web;  
using System.Web.UI;  
using System.Web.UI.WebControls;  
 
namespace WebApplication1  
{  
    public partial class contextmenu : System.Web.UI.UserControl  
    {  
        protected void Page_Load(object sender, EventArgs e)  
        {  
 
        }  
        protected override void OnPreRender(EventArgs e)  
        {  
            if (!this.Page.IsClientScriptBlockRegistered(this.GetType().FullName))  
                this.Page.RegisterClientScriptBlock(this.GetType().FullName, string.Format("<script type=\"text/javascript\" src=\"{0}\"></script>"this.ResolveUrl("~/ContextMenu.js")));  
 
            if (!Page.IsClientScriptBlockRegistered(this.ClientID))  
                Page.RegisterStartupScript(this.ClientID, String.Format("<script language=javascript>window.{0} = new ContextMenu('{0}', {1});</script>"this.ClientID, this.RadContextMenu1.ClientID));  
            base.OnPreRender(e);  
        }  
    }  
contextmenu.js
function ContextMenu(name, contextMenu) {  
    this.Name = name;  
    this.ContextMenuHandle = contextMenu;  
 
    this.ShowContextMenu = function() {  
        this.ContextMenuHandle.show(arguments[1]); //(the JScript error ) 
    }  
Any idea about what is wrong?

Thanks.

1 Answer, 1 is accepted

Sort by
0
Veronica
Telerik team
answered on 14 Apr 2010, 01:25 PM
Hello tother,

I guess you are using this help topic as a reference.

However in your case you place the RadContextMenu in a User Control. To be able to find the RadContextMenu in Default.aspx page you'll have to subscribe to the OnClientLoad event to get the menu as  a global variable:

<Telerik:RadContextMenu id="RadContextMenu1"  
                runat="server" OnClientLoad="load" >  
    <Items>  
        <Telerik:RadMenuItem Text="Trees" />  
        <Telerik:RadMenuItem Text="Sunset" />  
        <Telerik:RadMenuItem Text="Mountains" />  
    </Items>  
</Telerik:RadContextMenu>  
<script type="text/javascript">
var menu;
function load(sender, e)
{
   menu = sender;
}
</script>

After that you may use the code from the help topic to the Default page but assign contextMenu to the global menu variable like this:

<script type="text/javascript">
        function showMenu(e) {
            var contextMenu = menu;
  
            if ((!e.relatedTarget) || (!$telerik.isDescendantOrSelf(contextMenu.get_element(), e.relatedTarget))) {
                contextMenu.show(e);
            }
  
            $telerik.cancelRawEvent(e);
        }
  
    </script>

Find the full code in the attached .zip file.

Hope this helps.

Regards,
Veronica Milcheva
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
Tags
Menu
Asked by
tother
Top achievements
Rank 1
Answers by
Veronica
Telerik team
Share this question
or