New to Telerik UI for ASP.NET AJAX? Download free 30-day trial

Context Menu

Next database reset in 1 hours, 51 minutes, 14 seconds
OrderIDShipNameOrderDateShippedDateShipCountry
Ship city Show context menu
ShipPostalCodeFreight
       Sum : 60879.21
Page size:
 762 items in 51 pages
10307Lonesome Pine Restaurant9/17/1996Wednesday, September 25, 1996USA Portland 972190.56
10314Rattlesnake Canyon Grocery9/25/1996Friday, October 4, 1996italy 8711074.16
10315Island Trading9/26/1996Thursday, October 3, 1996UK Cowes PO31 7PJ41.76
10317Lonesome Pine Restaurant9/30/1996Thursday, October 10, 1996USA Portland 9721912.69
10319Tortuga Restaurante10/2/1996Friday, October 11, 1996Mexico México D.F. 0503364.50
10321Island Trading10/3/1996Friday, October 11, 1996UK Cowes PO31 7PJ3.43
10323Königlich Essen10/7/1996Monday, October 14, 1996Germany Brandenburg 147764.88
10324Save-a-lot Markets10/8/1996Thursday, October 10, 1996USA Boise 83720214.27
10325Königlich Essen10/9/1996Monday, October 14, 1996Germany Brandenburg 1477664.86
10326Bólido Comidas preparadas10/10/1996Monday, October 14, 1996Spain Madrid 2802377.92
10328Furia Bacalhau e Frutos do Mar10/14/1996Thursday, October 17, 1996Portugal Lisboa 167587.03
10330LILA-Supermercado10/16/1996Monday, October 28, 1996Venezuela Barquisimeto 350812.75
10331Bon app'10/16/1996Monday, October 21, 1996France Marseille 1300810.19
10332Mère Paillarde10/17/1996Monday, October 21, 1996Canada Montréal H1J 1C352.84
10333Wartian Herkku10/18/1996Friday, October 25, 1996Finland Oulu 901100.59
  • NoFilter
  • Contains
  • DoesNotContain
  • StartsWith
  • EndsWith
  • EqualTo
  • NotEqualTo
  • GreaterThan
  • LessThan
  • GreaterThanOrEqualTo
  • LessThanOrEqualTo
  • Between
  • NotBetween
  • IsEmpty
  • NotIsEmpty
  • IsNull
  • NotIsNull
  • Custom
  • Sort Ascending
  • Sort Descending
  • Clear Sorting
  • Group By
  • Ungroup
  • Columns
  • Filter
  • Add
  • Edit
  • Delete

This example demonstrates RadGrid's header context filter menu which is a sub-part or RadGrid's header context menu and can be switched on/off using RadGrid.EnableHeaderContextFilterMenu or GridTableView.EnableHeaderContextFilterMenu properties. In addition, there are two indispensable prerequisites for the new filtering feature to be available:

  • the column filtering has to be switched on through AllowColumnFiltering = true.
  • the header context menu must be enabled through EnableHeaderContextMenu = true.

The new filter menu offers the power for filtering the grid's data on two conditions related with a logical AND operator. Two additional properties have been introduced into the GridColumn type in order to support the second filter condition data:

  • AndCurrentFilterFunction.
  • AndCurrentFilterValue.

When a filter command is triggered from the header context filter menu, RadGrid will fire the RadGrid.HeaderContextMenuFilterCommand, raise the ItemCommand event and send into the event handlers the filter data through the event arguments object.

Additionally, in numerous cases you may want to display a context menu when right-clicking an arbitrary row in RadGrid. Through a command from that context menu you may want to change the state for the grid row (for example edit/delete/add record). Furthermore, you may prefer the operation with asynchronous request instead of postback.

The seamless integration between RadContextMenu, RadAjax and RadGrid makes this task very easy to be accomplished. Our grid control exposes OnRowContextMenu client event which can be handled to select the right-clicked record in the grid to mark it as active for the current action. In order to determine the index of the clicked row on the server, use a hidden field on the page to store its value client side and then operate with it on form submit (in the ItemClick event handler of RadMenu). Inside the handler perform the corresponding task chosen by the user to modify the item state.

To associate the context menu with the grid instance, attach the OnRowContextMenu client event of RadGrid, get reference to the context menu object client-side and invoke its show method (passing the browser's event argument as parameter).

Finally, configure the ajax manager settings in order to refresh the grid and the menu in a codeless manner.
  • RadGrid's header context menu can be shown either:
    • on mouse right-click over a header cell
    • or by left-clicking on an element that fires a client-side method of RadGrid's, showing the context menu (see the ShipCity template column)
  • RadGrid's row context menu can be shown by right clicking on each data row.
  • DefaultCS.aspx
  • DefaultCS.aspx.cs
  • scripts.js
<%@ Page Language="c#"  CodeFile="DefaultCS.aspx.cs" Inherits="Telerik.Web.Examples.Integration.GridAndMenu.DefaultCS" %>

<%@ Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %>
<!DOCTYPE html>
<html xmlns='http://www.w3.org/1999/xhtml'>
<head runat="server">
    <title>Telerik ASP.NET Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <telerik:RadScriptManager runat="server" ID="RadScriptManager1" />
    <telerik:RadSkinManager ID="RadSkinManager1" runat="server" ShowChooser="true" />
    <script type="text/javascript" src="scripts.js"></script>
    <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server" UpdateInitiatorPanelsOnly="true">
        <AjaxSettings>
            <telerik:AjaxSetting AjaxControlID="RadGrid1">
                <UpdatedControls>
                    <telerik:AjaxUpdatedControl ControlID="RadGrid1" LoadingPanelID="RadAjaxLoadingPanel1"></telerik:AjaxUpdatedControl>
                </UpdatedControls>
            </telerik:AjaxSetting>
        </AjaxSettings>
    </telerik:RadAjaxManager>
    <telerik:RadAjaxLoadingPanel ID="RadAjaxLoadingPanel1" runat="server">
    </telerik:RadAjaxLoadingPanel>
    <div class="demo-container no-bg">
    <telerik:RadGrid RenderMode="Lightweight" AutoGenerateColumns="false" ID="RadGrid1" DataSourceID="SqlDataSource1" Width="100%"
        AllowFilteringByColumn="True" AllowSorting="True" PageSize="15" ShowFooter="True"
        AllowPaging="True" runat="server" GridLines="None" EnableLinqExpressions="false"
        EnableHeaderContextMenu="true" EnableHeaderContextFilterMenu="true" OnPreRender="RadGrid1_PreRender">
        <PagerStyle Mode="NextPrevAndNumeric"></PagerStyle>
        <GroupingSettings CaseSensitive="false"></GroupingSettings>
        <MasterTableView AutoGenerateColumns="false" IsFilterItemExpanded="false" EditMode="InPlace" DataKeyNames="OrderID"
            AllowFilteringByColumn="True" ShowFooter="True" TableLayout="Auto" AllowAutomaticDeletes="true" AllowAutomaticInserts="true" AllowAutomaticUpdates="true">
            <Columns>
                <telerik:GridEditCommandColumn UniqueName="EditCommandColumn" Visible="false"></telerik:GridEditCommandColumn>
                <telerik:GridNumericColumn DataField="OrderID" HeaderText="OrderID" SortExpression="OrderID"
                    UniqueName="OrderID" ReadOnly="true">
                </telerik:GridNumericColumn>
                <telerik:GridBoundColumn DataField="ShipName" HeaderText="ShipName" SortExpression="ShipName"
                    UniqueName="ShipName">
                </telerik:GridBoundColumn>
                <telerik:GridDateTimeColumn DataField="OrderDate" HeaderText="OrderDate" SortExpression="OrderDate"
                    UniqueName="OrderDate" PickerType="None" DataFormatString="{0:d}">
                </telerik:GridDateTimeColumn>
                <telerik:GridDateTimeColumn DataField="ShippedDate" HeaderText="ShippedDate" SortExpression="ShippedDate"
                    UniqueName="ShippedDate" PickerType="DatePicker" DataFormatString="{0:D}">
                    <HeaderStyle Width="160px"></HeaderStyle>
                </telerik:GridDateTimeColumn>
                <telerik:GridBoundColumn DataField="ShipCountry" HeaderText="ShipCountry" SortExpression="ShipCountry"
                    UniqueName="ShipCountry">
                </telerik:GridBoundColumn>
                <telerik:GridTemplateColumn DataField="ShipCity" GroupByExpression="ShipCity Group by ShipCity"
                    UniqueName="ShipCity" InitializeTemplatesFirst="false" HeaderStyle-Width="100"
                    HeaderText="Ship city">
                    <HeaderTemplate>
                        <table>
                            <tr>
                                <td>
                                    <asp:LinkButton ID="lnkSort" runat="server" CommandArgument="ShipCity" CommandName="Sort"
                                        Text="Ship city"></asp:LinkButton>
                                </td>
                                <td>
                                    <img src="Img/Menu.png" style="margin-top: 5px; margin-left: 5px; cursor: pointer"
                                        onclick='demo.ShowColumnHeaderMenu(event,"ShipCity")' alt="Show context menu" />
                                </td>
                            </tr>
                        </table>
                    </HeaderTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lblCity" runat="server" Text='<%#Eval("ShipCity") %>'></asp:Label>
                    </ItemTemplate>
                </telerik:GridTemplateColumn>
                <telerik:GridMaskedColumn DataField="ShipPostalCode" HeaderText="ShipPostalCode"
                    SortExpression="ShipPostalCode" UniqueName="ShipPostalCode" Mask="#####">
                    <FooterStyle Font-Bold="true"></FooterStyle>
                </telerik:GridMaskedColumn>
                <telerik:GridNumericColumn DataField="Freight" DataType="System.Decimal" HeaderText="Freight"
                    SortExpression="Freight" UniqueName="Freight" Aggregate="Sum">
                    <FooterStyle Font-Bold="true"></FooterStyle>
                </telerik:GridNumericColumn>
            </Columns>
        </MasterTableView>
        <ClientSettings>
            <ClientEvents OnRowContextMenu="demo.RowContextMenu" />
            <Selecting AllowRowSelect="true" />
            <Scrolling AllowScroll="false"></Scrolling>
        </ClientSettings>
    </telerik:RadGrid>
        </div>
    <input type="hidden" id="radGridClickedRowIndex" name="radGridClickedRowIndex" />
    <telerik:RadContextMenu ID="RadMenu1" runat="server" OnItemClick="RadMenu1_ItemClick"
        EnableRoundedCorners="true" EnableShadows="true">
        <Items>
            <telerik:RadMenuItem Text="Add">
            </telerik:RadMenuItem>
            <telerik:RadMenuItem Text="Edit">
            </telerik:RadMenuItem>
            <telerik:RadMenuItem Text="Delete">
            </telerik:RadMenuItem>
        </Items>
    </telerik:RadContextMenu>
    <asp:SqlDataSource ID="SqlDataSource1" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>" SelectCommand="SELECT * FROM [Orders]" runat="server" DeleteCommand="DELETE FROM [Orders] WHERE [OrderID] = @OrderID" InsertCommand="INSERT INTO [Orders] ([CustomerID], [EmployeeID], [OrderDate], [RequiredDate], [ShippedDate], [ShipVia], [Freight], [ShipName], [ShipAddress], [ShipCity], [ShipRegion], [ShipPostalCode], [ShipCountry]) VALUES (@CustomerID, @EmployeeID, @OrderDate, @RequiredDate, @ShippedDate, @ShipVia, @Freight, @ShipName, @ShipAddress, @ShipCity, @ShipRegion, @ShipPostalCode, @ShipCountry)" UpdateCommand="UPDATE [Orders] SET [CustomerID] = @CustomerID, [EmployeeID] = @EmployeeID, [OrderDate] = @OrderDate, [RequiredDate] = @RequiredDate, [ShippedDate] = @ShippedDate, [ShipVia] = @ShipVia, [Freight] = @Freight, [ShipName] = @ShipName, [ShipAddress] = @ShipAddress, [ShipCity] = @ShipCity, [ShipRegion] = @ShipRegion, [ShipPostalCode] = @ShipPostalCode, [ShipCountry] = @ShipCountry WHERE [OrderID] = @OrderID">
        <DeleteParameters>
            <asp:Parameter Name="OrderID" Type="Int32" />
        </DeleteParameters>
        <InsertParameters>
            <asp:Parameter Name="CustomerID" Type="String" />
            <asp:Parameter Name="EmployeeID" Type="Int32" />
            <asp:Parameter Name="OrderDate" Type="DateTime" />
            <asp:Parameter Name="RequiredDate" Type="DateTime" />
            <asp:Parameter Name="ShippedDate" Type="DateTime" />
            <asp:Parameter Name="ShipVia" Type="Int32" />
            <asp:Parameter Name="Freight" Type="Decimal" />
            <asp:Parameter Name="ShipName" Type="String" />
            <asp:Parameter Name="ShipAddress" Type="String" />
            <asp:Parameter Name="ShipCity" Type="String" />
            <asp:Parameter Name="ShipRegion" Type="String" />
            <asp:Parameter Name="ShipPostalCode" Type="String" />
            <asp:Parameter Name="ShipCountry" Type="String" />
        </InsertParameters>
        <UpdateParameters>
            <asp:Parameter Name="CustomerID" Type="String" />
            <asp:Parameter Name="EmployeeID" Type="Int32" />
            <asp:Parameter Name="OrderDate" Type="DateTime" />
            <asp:Parameter Name="RequiredDate" Type="DateTime" />
            <asp:Parameter Name="ShippedDate" Type="DateTime" />
            <asp:Parameter Name="ShipVia" Type="Int32" />
            <asp:Parameter Name="Freight" Type="Decimal" />
            <asp:Parameter Name="ShipName" Type="String" />
            <asp:Parameter Name="ShipAddress" Type="String" />
            <asp:Parameter Name="ShipCity" Type="String" />
            <asp:Parameter Name="ShipRegion" Type="String" />
            <asp:Parameter Name="ShipPostalCode" Type="String" />
            <asp:Parameter Name="ShipCountry" Type="String" />
            <asp:Parameter Name="OrderID" Type="Int32" />
        </UpdateParameters>
    </asp:SqlDataSource>
    </form>
</body>
</html>

Support & Learning Resources

Find Assistance