Telerik
Home / Community / Forums / RadControls for ASP.NET: Menu / Dynamically building context menu using Atlas

Dynamically building context menu using Atlas

Feed from this thread
  • terrysmith Intermediate avatar

    Posted on Nov 16, 2006 (permalink)

    Hi,

    Do you have any examples of dynamically building a context menu using an Atlas call before the menu is displayed to the user?

    I need to change the context menu items based on what table cell a context menu is being displayed for and the logic for determining those items needs to be on the server. In other words, I can't use the menu's Javascript API in this instance.

    Thank you,
    Terry

    Reply

  • terrysmith Intermediate avatar

    Posted on Nov 16, 2006 (permalink)

    Below is a simple example of what I'm trying to get to work. I've put the radMenu and a "Build Context Menu" button inside of an Atlas UpdatePanel. My theory is that once I get the button to update the context menu asynchronously then I can hide the button and fire it from my Javascript method.

    However, I'm getting this error: "
    A JSONObject must begin with '{' ". I've seen forum posts with this error, but no one has posted a solution for it.

    Here are two scenerios with the sample code below that create this error:
    1. Click "Build Context Menu" to add a menu item using Atlas.
    2. Right-click on one of the table cells. The new menu item is not displayed.
    3. Click the "Page Post-back" button. An error page with the JSONObject error is displayed.
    4. Go back to the page. Click "Page Post-back" again. It works this time, does a whole-page refresh, and now the context menu displays all of the newly added items.

    1. Click "Build Context Menu" to add a menu item using Atlas.
    2. Click "Build Context Menu" a second time. IE displays the JSONObject error in a message box.

    Any help would be greatly appreciated.

    Thank you,
    Terry

    Default.aspx

    <%@ Page Language="VB" AutoEventWireup="true" CodeFile="Default.aspx.vb" Inherits="_Default" %>

    <%@ Register Assembly="RadMenu.Net2" Namespace="Telerik.WebControls" TagPrefix="radM" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml">

    <head runat="server">

    <title>Untitled Page</title>

    </head>

    <body>

    <script language="javascript">

    function dataCellClick(cellID)

    {

    var contextMenu = <%= radContextMenu.ClientID %>;

    contextMenu.Show(event);

    // Stop IE from showing its context menu.

    event.cancelBubble = true;

    if (event.stopPropagation)

    {

    event.stopPropagation();

    }

    event.returnValue = false;

    }

    </script>

    <form id="form1" runat="server">

    <asp:ScriptManager ID="ScriptManager1" EnablePartialRendering="true" runat="server" />

    <br />

    <div>

    <asp:Button ID="Button1" runat="server" Text="Page Post-back" />

    <asp:UpdatePanel ID="UpdatePanel1" runat="server">

    <ContentTemplate>

    <asp:Button ID="btnBuildContextMenu" Text="Build Context Menu" runat="server" />

    <radM:RadMenu ID="radContextMenu" IsContext="true" ContextMenuElementID="none" runat="server">

    <Items>

    <radM:RadMenuItem Text="Static menu item" runat="server" />

    </Items>

    </radM:RadMenu>

    <table>

    <tr>

    <td id="R1C1" oncontextmenu="dataCellClick('R1C1')">Row 1 Cell 1</td>

    <td id="R1C2" oncontextmenu="dataCellClick('R1C2')">Row 1 Cell 2</td>

    </tr>

    <tr>

    <td id="R2C1" oncontextmenu="dataCellClick('R2C1')">Row 2 Cell 1</td>

    <td id="R2C2" oncontextmenu="dataCellClick('R2C2')">Row 2 Cell 2</td>

    </tr>

    </table>

    </ContentTemplate>

    </asp:UpdatePanel>

    </div>

    </form>

    </body>

    </html>




    Default.aspx.vb

    Partial Class _Default

    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    Dim item As New Telerik.WebControls.RadMenuItem

    item.Text = "Added in Page_Load " & Now.ToString

    item.Value = Now.Millisecond

    radContextMenu.Items.Add(item)

    End Sub

    Protected Sub radContextMenu_ItemClick(ByVal sender As Object, ByVal e As Telerik.WebControls.RadMenuEventArgs) Handles radContextMenu.ItemClick

    System.Diagnostics.Debug.WriteLine(e.Item.Text & "clicked with value = " & e.Item.Value)

    End Sub

    Protected Sub btnBuildContextMenu_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnBuildContextMenu.Click

    System.Diagnostics.Debug.WriteLine("btnBuildContextMenu_Click")

    Dim item As New Telerik.WebControls.RadMenuItem

    item.Text = Now.ToString

    item.Value = Now.Millisecond

    radContextMenu.Items.Add(item)

    End Sub

    End Class



     

    Reply

  • terrysmith Intermediate avatar

    Posted on Nov 16, 2006 (permalink)

    I'm still tweaking but apparently its a problem with the "seemless Atlas integration". :) No worries... I never expected it to be seemless anyway. I replaced the Atlas ScriptManager and UpdatePanel with the radAjax versions of the same and everything pretty-much worked as desired. My only problem now is that after dynamically building the context menu it immediately hides itself instead of staying displayed. I'm sure this is a side-effect of the Ajax round-trip.

    Reply

  • Telerik Admin admin's avatar

    Posted on Nov 16, 2006 (permalink)

    Hi Terry,

    Does hooking the oncontextmenu event help in this case (as you've mentioned in this forum thread)?
    http://www.telerik.com/community/forums/thread/b311D-khdkc.aspx

    All the best,
    Rumen Stankov (MCSD.NET)
    the telerik team

    Reply

  • terrysmith Intermediate avatar

    Posted on Nov 17, 2006 (permalink)

    I'm still having the problem. After dynamically building the context menu it immediately hides itself instead of staying displayed. I've traced it down to the asynchronous round-trip. If I put a message box in the Javascript code that fires before the menu is displayed the it works fine. Obviously, this is not wha the user expects however. :) What I really need is the "jax" part without the "a". Is it possible to fire a synchronous "ajax" call with your radAjax library?

    Reply

  • Telerik Admin admin's avatar

    Posted on Nov 20, 2006 (permalink)

    Hi Terry,

    Indeed the context menu would hide when you click anywhere outside of it. Our r.a.d.ajax product does not support synchronous ajax request as well. I suggest you show the menu programmatically after the ajax update. For MS Ajax you can use the RegisterStartupScript method of the ScriptManager.

    Regards,
    Albert
    the telerik team

    Reply

  • Jason Speece avatar

    Posted on Mar 13, 2008 (permalink)

    FYI - I got this senario to work through another posting on this site. (BTW - great forum). I wanted to put this back out in case anyone is stuck on the same thing.

    http://www.telerik.com/community/code-library/submission/b311D-khmge.aspx

    Have a great day,
    Jason

    Reply

Powered by Sitefinity ASP.NET CMS

Contact Us | Site Feedback | Terms of Use | Privacy Policy
Copyright © 2002-2009 Telerik. All rights reserved.