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
30 Answers, 1 is accepted
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.
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
OnPreRenderComplete
' cannot be declared 'Overrides
' because it does not override a sub in a base classOnPreRenderComplete
.UserControls does not have OnPreRenderComplete, only Page does.
Regards,
Nikolay
the Telerik team
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
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
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.
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
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:
|
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
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>
<html xmlns=
"http://www.w3.org/1999/xhtml"
>
<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
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
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.
Yes! That did it!
I removed the if construction
if (args.get_item().get_text() === "new item")
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
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?
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.
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...
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.
http://www.telerik.com/help/aspnet-ajax/grid-header-context-menu.html
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".
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...
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...
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. :)
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
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