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

Dynamically Created RadContextMenu not rendering Items

1 Answer 64 Views
Menu
This is a migrated thread and some comments may be shown as answers.
Fergal
Top achievements
Rank 1
Fergal asked on 31 Oct 2012, 02:21 PM
Hi,

I'm attempting to dynamically create a RadContextMenu inside an asp.net server control. Whilst the Context Menu appears to be created/rendered correctly, I am unable to get any menu items to actually create. Checking the rendered markup - it appears that these items never actually make it onto the page.

Below is a sample of my code, can anyone indicate what I am doing wrong?

Imports System.Web.UI.WebControls
Imports Telerik.Web.UI
 
<ParseChildren(True, "MenuItems")>
Public Class SplitButton
    Inherits WebControl
 
    Private _MenuItems As New List(Of MenuItem)
    Private _Button As RadButton
    Private _ContextMenu As RadContextMenu
 
    Public Property MenuItems() As List(Of MenuItem)
        Get
            Return _MenuItems
        End Get
        Set(ByVal value As List(Of MenuItem))
            _MenuItems = value
        End Set
    End Property
 
    Public Property Text As String
    Public Property IconUrl As String
 
    Private Sub BuildContextMenu()
        _ContextMenu = New RadContextMenu() With {.ID = String.Format("{0}_Menu", Me.ID)}
        Me.Controls.Add(_ContextMenu)
 
        Dim x As New List(Of RadMenuItem)
 
        For Each item As MenuItem In MenuItems
            Dim menuItem As New RadMenuItem() With {.Text = item.Text, .Value = item.Value, .Visible = item.Visible, .ImageUrl = item.IconUrl, .PostBack = item.PostBack}
 
            x.Add(menuItem)
        Next
 
        _ContextMenu.Items.AddRange(x)
 
 
    End Sub
 
    Private Sub BuildButton()
        _Button = New RadButton() With {.ID = String.Format("{0}_Button", Me.ID)}
 
        _Button.OnClientClicked = "SplitButton_OnClientClicked"
        _Button.Attributes.Add("data-contextmenu", _ContextMenu.ClientID)
        _Button.Text = Text
        _Button.Icon.PrimaryIconUrl = IconUrl
        _Button.EnableSplitButton = True
 
        Me.Controls.Add(_Button)
    End Sub
 
    Private Sub SplitButton_Init(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Init
        BuildContextMenu()
    End Sub
 
    Private Sub SplitButton_PreRender(ByVal Sender As Object, ByVal e As EventArgs) Handles Me.PreRender
 
        BuildButton()
    End Sub
 
End Class
 
Public Class MenuItem
 
    Public Property IconUrl As String
    Public Property Text As String
    Public Property Value As String
    Public Property PostBack As Boolean
    Public Property Visible As Boolean
 
End Class

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm1.aspx.vb" Inherits="Grid_SplitButtonDemo.WebForm1" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<%@ Register Assembly="GridSplitButtonDemo" Namespace="Grid_SplitButtonDemo" TagPrefix="a" %>
 
<head runat="server">
    <title>SplitButton Demo</title>
    <script type="text/javascript">
        function SplitButton_OnClientClicked(sender, args)
        {
            //Check if we clicked the splitbutton portion, or the button portion
            if (args.IsSplitButtonClick())
            {
                //Show the context menu below the selected button.
                var currentLocation = $telerik.getLocation(sender.get_element());
                var contextMenu = $find(sender.get_element().getAttribute('data-contextmenu'));
                //alert(contextMenu.get_items().get_count());
                contextMenu.showAt(currentLocation.x, currentLocation.y + 22);
 
                //Prevent postback.
                sender.set_autoPostBack(false);
            }
            else
            {
                //Button portion clicked, Perform the default action.
                alert("Perform Default Action");
                sender.set_autoPostBack(true);
            }
        }
    </script>
</head>
<body>   
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <h2>
        SplitButton Demo
    </h2>
    <div>
        <a:SplitButton runat="server" ID="btnActions" Text="Edit" IconUrl="Images/pencil.png">
            <a:MenuItem IconUrl="Images/Remove.png" Text="Delete" Value="Delete" PostBack="false" />
            <a:MenuItem IconUrl="Images/wand.png" Text="Add Another" Value="NewAnother" />
        </a:SplitButton>
    </div>
    </form>
</body>
</html>

Thanks
Fergal

1 Answer, 1 is accepted

Sort by
0
Kate
Telerik team
answered on 05 Nov 2012, 02:01 PM
Hello Fergal,

Attached to this post you can find a very simplified runnable project where I implemented a simple textbox with a context menu in a user control.

Greetings,
Kate
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Tags
Menu
Asked by
Fergal
Top achievements
Rank 1
Answers by
Kate
Telerik team
Share this question
or