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

Context Menu

Next database reset in 1 hours, 20 minutes, 6 seconds
OrderIDShipNameOrderDateShippedDateShipCountry
Ship city Show context menu
ShipPostalCodeFreight
       Sum : 61988.16
Page size:
 781 items in 53 pages
10295Vins et alcools Chevalier9/2/1996Tuesday, September 10, 1996France Reims 511001.15
10297Blondel père et fils9/4/1996Tuesday, September 10, 1996France Strasbourg 670005.74
10298Hungry Owl All-Night Grocers9/5/1996Wednesday, September 11, 1996Ireland Cork  168.22
10302Suprêmes délices9/10/1996Wednesday, October 9, 1996Belgium Charleroi B-60006.27
10303Godos Cocina Típica9/11/1996Wednesday, September 18, 1996Spain 41101107.83
10305Old World Delicatessen9/13/1996Wednesday, October 9, 1996USA Anchorage 99508257.62
10307Lonesome Pine Restaurant9/17/1996Wednesday, September 25, 1996USA Portland 972190.56
10308Ana Trujillo Emparedados y helados9/18/1996Tuesday, September 24, 1996Mexico México D.F. 050211.61
10309Hungry Owl All-Night Grocers9/19/1996Wednesday, October 23, 1996Ireland Cork  47.30
10310The Big Cheese9/20/1996Friday, September 27, 1996USA Portland 9720117.52
10312Die Wandernde Kuh9/23/1996Thursday, October 3, 1996Germany Stuttgart 7056340.26
10313QUICK-Stop9/24/1996Friday, October 4, 1996Germany Cunewalde 013071.96
10314Rattlesnake Canyon Grocery9/25/1996Friday, October 4, 1996USA Albuquerque 8711074.16
10315Island Trading9/26/1996Thursday, October 3, 1996UK Cowes PO31 7PJ41.76
10316Rattlesnake Canyon Grocery9/27/1996Tuesday, October 8, 1996USA Albuquerque 87110150.15
  • 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