HeaderContextMenu add/remove items vb.net

31 posts, 0 answers
  1. mabs
    mabs avatar
    57 posts
    Member since:
    May 2010

    Posted 27 Jul 2010 Link to this post

    I have successfully been able to hide items from the context menu.  Is it possible to remove the separator bars that also appear in the menu?  Also I am struggling to be able to add my own items.  I have seen the examples in c#, however these do not translate well to vb.  Below is the code I am using to hide the items.  It would be much appreciated if someone could show me how to add new items.  Once added I would also need to know how to capture the fired event when the menu item is clicked.

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
     
            AddHandler RadGrid1.HeaderContextMenu.ItemCreated, AddressOf Me.HeaderContextMenu_ItemCreated
     
         ...
    End Sub
     
    Private Sub HeaderContextMenu_ItemCreated(ByVal sender As Object, ByVal e As RadMenuEventArgs)
            Select Case (e.Item.Text)
                Case "Clear Sorting"
                    e.Item.Visible = False
                Case "Sort Ascending"
                    e.Item.Visible = False
                Case "Sort Descending"
                    e.Item.Visible = False
            End Select
    End Sub
  2. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 27 Jul 2010 Link to this post

    Hello Russell,

    You can try the following two approaches to hide the seperator in RadContextMenu.

    VB.Net
    Private Sub HeaderContextMenu_ItemCreated(sender As Object, e As RadMenuEventArgs)
        Dim menu As RadContextMenu = RadGrid1.HeaderContextMenu
        For Each item As RadMenuItem In menu.Items
            If item.IsSeparator Then
                item.Visible = False
            End If
        Next
        Select Case (e.Item.Text)
            Case "Clear Sorting"
                e.Item.Visible = False
                Exit Select
            Case "Sort Ascending"
                e.Item.Visible = False
                Exit Select
            Case "Sort Descending"
                e.Item.Visible = False
                Exit Select
        End Select
    End Sub

    Or you can add the following CSS style:

    CSS:
    <style type="text/css">
      .rmSeparator
        {
           display: none !important;
        }
    </style>

    In order to add new items to RadContextMenu try the following code snippet.

    VB.Net:
    Protected Overrides Sub OnPreRenderComplete(e As EventArgs)
        Dim menu As RadContextMenu = RadGrid1.HeaderContextMenu
        Dim item As New RadMenuItem()
        item.Text = "Google"
        item.NavigateUrl = "http://www.google.com"
        menu.Items.Add(item)
        MyBase.OnPreRenderComplete(e)
    End Sub

    Thanks,
    Princy.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. mabs
    mabs avatar
    57 posts
    Member since:
    May 2010

    Posted 27 Jul 2010 Link to this post

    Thank you for yout reply.

    I have an issue with regards to adding extra items.  I have come across the same bit of code in c#, when converted to vb, it does not work.  In the simplist form,
    Protected Overrides Sub OnPreRenderComplete(ByVal e As System.EventArgs)
     
    End Sub
    gives an error: sub 'OnPreRenderComplete' cannot be declared 'Overrides' because it does not override a sub in a base class

  5. Diego
    Diego avatar
    14 posts
    Member since:
    Jun 2010

    Posted 01 Aug 2010 Link to this post

    This looks like a general ASP.NET problem. What is MyBase in this context - a page, an user control, or something else?
  6. mabs
    mabs avatar
    57 posts
    Member since:
    May 2010

    Posted 02 Aug 2010 Link to this post

    Thanks for the reply.  It is in a userControl, never used OnPreRenderComplete.
  7. Nikolay Rusev
    Admin
    Nikolay Rusev avatar
    2285 posts

    Posted 05 Aug 2010 Link to this post

    Hi Russell,

    UserControls does not have OnPreRenderComplete, only Page does.

    Regards,
    Nikolay
    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
  8. mabs
    mabs avatar
    57 posts
    Member since:
    May 2010

    Posted 05 Aug 2010 Link to this post

    Thank you for the reply, so how would I add items to a context menu in a userControl?
  9. Nikolay Rusev
    Admin
    Nikolay Rusev avatar
    2285 posts

    Posted 10 Aug 2010 Link to this post

    Hello Russell,

    You can handler Page.PreRenderComplete event of current page within other event of your UserControl. For example OnLoad:

    Page.PreRenderComplete += new EventHandler(Page_PreRenderComplete);

    Best wishes,
    Nikolay
    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
  10. mabs
    mabs avatar
    57 posts
    Member since:
    May 2010

    Posted 10 Aug 2010 Link to this post

    Thanks for the reply.  I don't think I fully understand, I cant add the eventHandler PreRenderComplete in the userControl.  Is there an example anywhere of adding to the context menu when creating a usercontrol for the RadGrid?  I do not want to add any individual code to any page that uses this userControl (We are talking 100s), just a simple solution of adding a couple of items to the context menu.
  11. Nikolay Rusev
    Admin
    Nikolay Rusev avatar
    2285 posts

    Posted 13 Aug 2010 Link to this post

    Hello Russell,

    Every Control object has reference to the Page in which Controls collection is added. You can handler Page.PreRenderComplete event within your user control, not on every page that utilize that control.
    public partial class WebUserControl : System.Web.UI.UserControl
    {
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            Page.PreRenderComplete+=new EventHandler(Page_PreRenderComplete);
        }
    ....
    }


    Regards,
    Nikolay
    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
  12. mabs
    mabs avatar
    57 posts
    Member since:
    May 2010

    Posted 13 Aug 2010 Link to this post

    Thank you for the reply.

    I have converted to vb and have tried to add to my control,
    Protected Overrides Sub OnLoad(ByVal e As EventArgs)
        MyBase.OnLoad(e)
        AddHandler Page.PreRenderComplete, New EventHandler(Page_PreRenderComplete)
    End Sub

    However I am recieving an error for Page_PreRenderComplete when adding new eventHandler, the error is
    Delegate 'System.EventHandler' requires an 'AddressOf' expression or lambda expression as the only argument to its constructor.  

    I am sorry I am at a loss with this.
  13. Nikolay Rusev
    Admin
    Nikolay Rusev avatar
    2285 posts

    Posted 19 Aug 2010 Link to this post

    Russell,

    Please follow the link bellow in order to explore how to attach handlers in VB.NET
    http://msdn.microsoft.com/en-us/library/7taxzxka%28VS.80%29.aspx

    Nikolay
    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
  14. Erik
    Erik avatar
    314 posts
    Member since:
    Feb 2008

    Posted 09 Apr 2011 Link to this post

    Hi Nikolay

    When you ommit the item.NavigateUrl there is an error when you click the item... I want to handle a postback (click at added menu item align right for i.e.)

    The reason for the error I understand:

    Object reference not set to an instance of an object

    Stack Trace:

    [NullReferenceException: Object reference not set to an instance of an object.]
       System.Web.UI.Control.FindControl(String id, Int32 pathOffset) +219
       System.Web.UI.Page.FindControl(String id) +38
       Telerik.Web.UI.RadGrid.headerContextMenu_ItemClick(Object sender, RadMenuEventArgs e) +246
       Telerik.Web.UI.RadMenu.RaiseMenuItemEvent(Object eventKey, RadMenuEventArgs e) +123
       Telerik.Web.UI.RadMenu.OnItemClick(RadMenuEventArgs e) +42
       Telerik.Web.UI.RadMenu.RaiseItemClick(ControlItem item) +105
       Telerik.Web.UI.RadMenu.RaisePostBackEvent(String nodeIndex) +79
       Telerik.Web.UI.RadMenu.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String nodeIndex) +42
       System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
       System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +176
       System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5563
    

    This is probably due to the fact that the control is not there yet when headerContextMenu_ItemClick is executed. 

    but I can't seem to intervie or handle it myself...

    Reproduce:
    Add a radgrid, a datasource and enable headerContextMenu. Add the item as in your example, but leave the NavigateUrl property blanc.

    I've tried to add the control, at postback, in the load or init, but no luck...

    Regards,

    Erik
  15. Veli
    Admin
    Veli avatar
    2002 posts

    Posted 11 Apr 2011 Link to this post

    Hi Erik,

    The exception you are getting is caused because RadGrid expects each context menu item to have an attribute with name "TableID" containing the UniqueID of either the RadGrid instance or the respective GridTableView. This value is used internally by RadGrid. In your case, you can use RadGrid.UniqueID. Here is a sample test page in VB.NET that demonstrates how you can add custom context menu items and handle the ItemClick event to implement your custom logic on postback:

    <%@ Page Language="VB" %>
     
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
     
    <script runat="server">
        Protected Sub RadGrid1_NeedDataSource(ByVal sender As Object, ByVal e As GridNeedDataSourceEventArgs) Handles RadGrid1.NeedDataSource
            RadGrid1.DataSource = New String() {"a", "b", "c"}
        End Sub
         
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
            AddHandler RadGrid1.HeaderContextMenu.ItemClick, AddressOf HeaderContextMenu_ItemClick
            AddHandler RadGrid1.HeaderContextMenu.PreRender, AddressOf HeaderContextMenu_PreRender
        End Sub
         
        Protected Sub HeaderContextMenu_ItemClick(ByVal sender As Object, ByVal e As RadMenuEventArgs)
            Response.Write(e.Item.Text)
        End Sub
         
        Protected Sub HeaderContextMenu_PreRender(ByVal sender As Object, ByVal e As EventArgs)
            Dim newItem As New RadMenuItem("new item")
            newItem.Attributes("TableID") = RadGrid1.UniqueID
            RadGrid1.HeaderContextMenu.Items.Add(newItem)
        End Sub
    </script>
     
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <div>
            <telerik:RadGrid ID="RadGrid1" runat="server" Width="800px"
                EnableHeaderContextMenu="true">
            </telerik:RadGrid>
        </div>
        </form>
    </body>
    </html>

    In the above example, I attach to 2 context menu events - PreRender and ItemClick. I use PreRender to add my custom items (that also contain a TableID attribute). The ItemClick event is used to handle the postback action of my custom item - this is where your custom logic goes.

    Veli
    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
  16. Erik
    Erik avatar
    314 posts
    Member since:
    Feb 2008

    Posted 12 Apr 2011 Link to this post

    Hi Veli,

    Yes, that's better! Works fine.

    Now the obvious question: how do you know what column was right clicked...

    found some info that points to an attribute "ColumnName", but the only attribute in it is "TableID", of course.

    I've tried this and looked trough all objects in the event, but no luck...

    Dim rmi As RadMenuItem = e.Item
    Dim TableID As String = e.Item.Attributes("TableID")
    Dim ColumnName As String = e.Item.Attributes("ColumnName")
    Dim tableView As GridTableView = DirectCast(Me.Page.FindControl(TableID), GridTableView)
    Dim column As GridBoundColumn = DirectCast(tableView.GetColumn(ColumnName), GridBoundColumn)
    Select Case rmi.Value
        Case "cmd_...."

    Thanks in advance,

    Erik

  17. Veli
    Admin
    Veli avatar
    2002 posts

    Posted 13 Apr 2011 Link to this post

    The way RadGrid works is it internally sets the ColumnName attribute to the clicked item on the client. This is how the server side knows which column was clicked. You can take the same approach. You need 2 client-side events:

    1. RadGrid.ClientSettings.ClientEvents.OnColumnContextMenu - this will be used to save the unique name of the column for which the context menu is shown
    2. RadGrid.HeaderContextMenu.OnClientItemClicking - this will be used to set the ColumnName attribute to the clicked item

    Here is the javascript:

    <telerik:RadGrid ID="RadGrid1" runat="server" Width="800px"
        EnableHeaderContextMenu="true" Skin="Hay">
        <HeaderContextMenu OnClientItemClicking="contextMenuItemClicking">               
        </HeaderContextMenu>
        <ClientSettings>
            <ClientEvents OnColumnContextMenu="columnContextMenu" />
        </ClientSettings>
    </telerik:RadGrid>
     
    <script type="text/javascript">
        var columnName;
     
        function columnContextMenu(sender, args)
        {
            columnName =args.get_gridColumn().get_uniqueName();
        }
     
        function contextMenuItemClicking(sender, args)
        {
            if (args.get_item().get_text() === "new item")
            {
                sender.trackChanges();
                args.get_item().get_attributes().setAttribute("ColumnName", columnName);
                sender.commitChanges();
            }
        }
    </script>

    Now we have info as to which grid column was clicked. We have the column UniqueName as a ColumnName attribute in the clicked item. Attaching the full test page to demonstrate this approach.

    Veli
    the Telerik team

    Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.

  18. Erik
    Erik avatar
    314 posts
    Member since:
    Feb 2008

    Posted 14 Apr 2011 Link to this post

    Hi Veli,

    Yes! That did it!

    I removed the if construction
    if (args.get_item().get_text() === "new item")
    Then it automatically  works for all new items.

    I anyone is interested, I build it completely in code, so the radgrid and the headercontext menu adjustment (column alignment). You can contact me.

    Thanks Veli & rest. I think this post now covers all issues.

    (RadGrid Version: 2011.315)

    Regards,

    Erik

  19. July
    July avatar
    205 posts
    Member since:
    Jul 2011

    Posted 03 Nov 2011 Link to this post

    Hello! I need save the preferences of user.
    I user hide X columns from Header Context menu, order, etcc I need save it.
    which is the events of Header Cotext menu, that i can use for that?

  20. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 04 Nov 2011 Link to this post

    Hello Julieta,

    One suggestion is save the value in a HiddenField. Try the following code.

    JS:
    <script type="text/javascript">
     function OnColumnHiding(Sender, args)
     {
      alert(args.get_gridColumn().get_uniqueName());
     }
    </script>

    Thanks,
    Shinu.
  21. Quattro Formaggi
    Quattro Formaggi avatar
    18 posts
    Member since:
    Jun 2011

    Posted 11 Apr 2012 Link to this post

    Hi,

    I'm trying to add a new item under the "Columns" item of the HeaderContextMenu. Eventhough the item gets added to the items collection, it is not visible in the Columns menu. I'm using the following code to add the item:

    Private Sub HeaderContextMenuOnPreRender(sender As Object, e As EventArgs)
                Dim headerContextMenuItemColumns As RadMenuItem = GridReport.HeaderContextMenu.Items.FindItemByText("Columns")
     
                If headerContextMenuItemColumns IsNot Nothing Then
                    'Add the "Check All" item
     
                    With headerContextMenuItemColumns
                        Dim checkAllColumnsMenuItem As New RadMenuItem
                        Dim checkAll As New CheckBox
     
                        With checkAll
                            .ID = "chk" & GridReport.MasterTableView.UniqueID & "Check All"
                            .Text = "  Check All"
                            .Attributes.Add("onclick", "CheckAllColumns('" & GridReport.HeaderContextMenu.ClientID & "', 1);") 'This is my custom javascript function to check all checkboxes under the Columns menu
                            .Style("white-space") = "nowrap"
                        End With
     
                        With checkAllColumnsMenuItem
                            .Text = "  Check All"
                            .Value = GridReport.MasterTableView.ClientID & "|Check All"
                            .Attributes("TableID") = GridReport.MasterTableView.UniqueID
                            .Controls.Add(checkAll)
                            .PostBack = False
                        End With
     
                        .Items.Insert(0, checkAllColumnsMenuItem)
                    End With
                End If
            End Sub

    Can anybody tell me what I'm doing wrong? If I use the same code to add an item at the first level of the HeaderContextMenu, the item with the check box gets added successfully. Why doesn't the code work when trying to add an item at the second level? Thanks in advance for any contribution...

  22. Quattro Formaggi
    Quattro Formaggi avatar
    18 posts
    Member since:
    Jun 2011

    Posted 22 Apr 2012 Link to this post

    I still need help on the issue I've explained in my previous post. Please help...
  23. Sacha
    Sacha avatar
    7 posts
    Member since:
    Apr 2011

    Posted 24 Apr 2012 Link to this post

    Hi Quattro, If you add the items in the Pages OnPreRenderComplete event do they show up correctly?
  24. Quattro Formaggi
    Quattro Formaggi avatar
    18 posts
    Member since:
    Jun 2011

    Posted 24 Apr 2012 Link to this post

    Hi Sacha,

    No, the behaviour is the same in both the pages OnPreRenderComplete event and the HeaderContextMenus OnPreRender event. If I use the same code to add items under menu items other than the Columns menu item, the code works correctly.
  25. Sacha
    Sacha avatar
    7 posts
    Member since:
    Apr 2011

    Posted 24 Apr 2012 Link to this post

    Hi Quattro, I see what you mean, have you tried this dynamic example, should accomplish what you need.

    http://www.telerik.com/help/aspnet-ajax/grid-header-context-menu.html
  26. Quattro Formaggi
    Quattro Formaggi avatar
    18 posts
    Member since:
    Jun 2011

    Posted 24 Apr 2012 Link to this post

    Hi Sacha,

    Thank you for the link. I've already read that documentation part but it didn't help. As far as I've understood, the newly added menu items in that example are related with existing columns. But in my case, I want to add an independent menu item called "Check All".
  27. Quattro Formaggi
    Quattro Formaggi avatar
    18 posts
    Member since:
    Jun 2011

    Posted 24 Apr 2012 Link to this post

    Hi again Sacha,

    You can find a simple example project that shows the issue I'm facing at this address: http://www.2shared.com/file/WA-Up_VZ/HeaderContextMenu.html (Don't forget to add a reference to the Telerik.Web.UI.dll assembly in the project)
    Maybe you can have a look at it and tell me what I'm doing wrong... Thanks in advance...
  28. Quattro Formaggi
    Quattro Formaggi avatar
    18 posts
    Member since:
    Jun 2011

    Posted 28 Apr 2012 Link to this post

    I've noticed something very strange related with this issue. Run my sample project and in the Internet Explorer window, right click and click "View source". You will see that the "Check All" item is there. It must be a client script that prevents it from displaying. Isn't there a Telerik guy who can help me with this issue?
  29. Quattro Formaggi
    Quattro Formaggi avatar
    18 posts
    Member since:
    Jun 2011

    Posted 29 Apr 2012 Link to this post

    Hi,

    I've examined the RadControls source code and solved the issue myself. The reason for the MenuItem not being shown was that it was not related to a column and so it was made invisible using the set_visible(false) function. To make it visible again, I had to call the set_visible(true) function for that MenuItem in the OnClientShowing event handler on the client side.

    Sacha, you were the only one who tried to help me. Thank you for your interest on my issue...
  30. Sacha
    Sacha avatar
    7 posts
    Member since:
    Apr 2011

    Posted 30 Apr 2012 Link to this post

    Hi Four Cheese,

    Apologies for my late response, I was away during the weekend.
    Great that you managed to find a solution to this and shared it with the community. :)
  31. Jon
    Jon avatar
    53 posts
    Member since:
    Oct 2013

    Posted 23 Aug Link to this post

    I couldn't get this to work as the e.Item.Text values returned were the column names, not the menu items! In my case, I wanted to prevent users from hiding the columns so I wanted to remove this functionality from the HeaderContectMenu. After a bit of digging and experimentation, I came up with the following:

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
     
            ' Add a PreRender event handler to the HeaderContextMenu. We're going to use this
            ' to hide the HeaderContextMenu option which allows users to hide/show columns as we don't want them
            ' to be able to do this.
            AddHandler RadGrid1.HeaderContextMenu.PreRender, AddressOf HeaderContextMenu_PreRender
     
        End Sub ' Page_Load
     
        Protected Sub HeaderContextMenu_PreRender(ByVal sender As Object, ByVal e As EventArgs)
     
            ' Get the HeaderContextMenu
            Dim menu As GridHeaderContextMenu = sender
     
            ' Get the items in the HeaderContextMenu
            Dim menuitems As RadMenuItemCollection = menu.Items
     
            ' Iterate through the items in the HeaderContextMenu ...
            For Each item As RadMenuItem In menuitems
     
                ' Until we find the ColumnsContainer item ...
                If item.Value = "ColumnsContainer" Then
                    item.Visible = False ' which we'll then hide
                    Exit For ' No need to continue the iteration loop
                End If
     
            Next
     
        End Sub ' HeaderContextMenu_PreRender

Back to Top
UI for ASP.NET Ajax is Ready for VS 2017