Context Menu in radgrid from server side

5 posts, 0 answers
  1. Dorababu
    Dorababu avatar
    95 posts
    Member since:
    May 2012

    Posted 07 Jul 2012 Link to this post

    How can I handle the selected event to fire the corresponding operation. Assume I have two Menu items namely Edit and Delete when I select edit and update the record I would like to fire the RadGrid_UpdateCommand event and when I select Delete I would like to fire RadGrid_DeleteCommand
  2. Dorababu
    Dorababu avatar
    95 posts
    Member since:
    May 2012

    Posted 07 Jul 2012 Link to this post

    Editing record is working fine what should I write for deleting when selecting an Item from menu

    case "Delete":
                    int index1 = Convert.ToInt16(radGridClickedRowIndex);
                    if (index1== 0)
                    {
                        RadGrid2.MasterTableView.PerformDelete(RadGrid2.Items[radGridClickedRowIndex]);
                    }
                    else
                    {
                        RadWindowManager2.RadAlert("Sorry you can not Delete this record.", 300, 50, "", "");
                    }
                    break;
  3. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 09 Jul 2012 Link to this post

    Hi Dorababu,

    Please take a look into the following code snippet i tried  to achieve the same scenario.

    ASPX:
    <telerik:RadScriptManager ID="ScriptManager1" runat="server" />
        <telerik:RadCodeBlock runat="server" ID="RadCodeBlock1">
            <script type="text/javascript">
                function RowContextMenu(sender, eventArgs) {
                    var menu = $find("<%=RadMenu1.ClientID %>");
                    var evt = eventArgs.get_domEvent();
     
                    if (evt.target.tagName == "INPUT" || evt.target.tagName == "A") {
                        return;
                    }
     
                    var index = eventArgs.get_itemIndexHierarchical();
                    document.getElementById("radGridClickedRowIndex").value = index;
     
                    sender.get_masterTableView().selectItem(sender.get_masterTableView().get_dataItems()[index].get_element(), true);
     
                    menu.show(evt);
     
                    evt.cancelBubble = true;
                    evt.returnValue = false;
     
                    if (evt.stopPropagation) {
                        evt.stopPropagation();
                        evt.preventDefault();
                    }
                }
            </script>
        </telerik:RadCodeBlock>
        <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server" EnableAJAX="false">
            <AjaxSettings>
                <telerik:AjaxSetting AjaxControlID="RadGrid1">
                    <UpdatedControls>
                        <telerik:AjaxUpdatedControl ControlID="RadGrid1" LoadingPanelID="RadAjaxLoadingPanel1" />
                        <telerik:AjaxUpdatedControl ControlID="RadMenu1" />
                    </UpdatedControls>
                </telerik:AjaxSetting>
                <telerik:AjaxSetting AjaxControlID="RadMenu1">
                    <UpdatedControls>
                        <telerik:AjaxUpdatedControl ControlID="RadGrid1" LoadingPanelID="RadAjaxLoadingPanel1" />
                        <telerik:AjaxUpdatedControl ControlID="RadMenu1" />
                    </UpdatedControls>
                </telerik:AjaxSetting>
            </AjaxSettings>
        </telerik:RadAjaxManager>
        <telerik:RadAjaxLoadingPanel ID="RadAjaxLoadingPanel1" runat="server" />
        <input type="hidden" id="radGridClickedRowIndex" name="radGridClickedRowIndex" />
        <div style="margin-right: 20px;">
            <telerik:RadGrid ID="RadGrid1" runat="server" Width="100%" DataSourceID="SqlDataSource1"
                AllowAutomaticDeletes="true" AllowAutomaticInserts="true" AllowAutomaticUpdates="true"
                OnPreRender="RadGrid1_PreRender" OnUpdateCommand="RadGrid1_UpdateCommand">
                <MasterTableView AllowSorting="False" PageSize="10" AllowPaging="True" Width="100%"
                    DataKeyNames="ProductID" DataSourceID="SqlDataSource1" EditMode="InPlace">
                    <Columns>
                        <telerik:GridEditCommandColumn UniqueName="EditCommandColumn" Visible="false" />
                    </Columns>
                </MasterTableView>
                <ClientSettings>
                    <ClientEvents OnRowContextMenu="RowContextMenu"></ClientEvents>
                    <Selecting AllowRowSelect="true" />
                </ClientSettings>
                <PagerStyle Mode="NextPrevAndNumeric" />
            </telerik:RadGrid>
        </div>
        <br />
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString3 %>"
            DeleteCommand="DELETE FROM [Products] WHERE [ProductID] = @ProductID" InsertCommand="INSERT INTO [Products] ([ProductName], [UnitPrice], [UnitsInStock]) VALUES (@ProductName, @UnitPrice, @UnitsInStock)"
            SelectCommand="SELECT [ProductID], [ProductName], [UnitPrice], [UnitsInStock] FROM [Products]"
            UpdateCommand="UPDATE [Products] SET [ProductName] = @ProductName, [UnitPrice] = @UnitPrice, [UnitsInStock] = @UnitsInStock WHERE [ProductID] = @ProductID">
            <DeleteParameters>
                <asp:Parameter Name="ProductID" Type="Int32" />
            </DeleteParameters>
            <InsertParameters>
                <asp:Parameter Name="ProductName" Type="String" />
                <asp:Parameter Name="UnitPrice" Type="Decimal" />
                <asp:Parameter Name="UnitsInStock" Type="Int16" />
            </InsertParameters>
            <UpdateParameters>
                <asp:Parameter Name="ProductName" Type="String" />
                <asp:Parameter Name="UnitPrice" Type="Decimal" />
                <asp:Parameter Name="UnitsInStock" Type="Int16" />
                <asp:Parameter Name="ProductID" Type="Int32" />
            </UpdateParameters>
        </asp:SqlDataSource>
        <telerik:RadContextMenu ID="RadMenu1" runat="server" OnItemClick="RadMenu1_ItemClick"
            EnableRoundedCorners="true" EnableShadows="true">
            <Items>
                <telerik:RadMenuItem Text="Add" />
                <telerik:RadMenuItem Text="Edit" />
                <telerik:RadMenuItem Text="Delete" />
            </Items>
        </telerik:RadContextMenu>

    C#:
    protected void RadGrid1_PreRender(object sender, EventArgs e)
    {
        if (RadGrid1.EditIndexes.Count > 0 || RadGrid1.MasterTableView.IsItemInserted)
        {
            GridColumn col1 = RadGrid1.MasterTableView.GetColumn("EditCommandColumn");
            col1.Visible = true;
        }
        else
        {
            GridColumn col2 = RadGrid1.MasterTableView.GetColumn("EditCommandColumn");
            col2.Visible = false;
        }
        (RadGrid1.MasterTableView.AutoGeneratedColumns[0] as GridBoundColumn).ReadOnly = true;
    }
    protected void RadMenu1_ItemClick(object sender, RadMenuEventArgs e)
    {
        int radGridClickedRowIndex;
     
        radGridClickedRowIndex = Convert.ToInt32(Request.Form["radGridClickedRowIndex"]);
     
        switch (e.Item.Text)
        {
            case "Edit":
                RadGrid1.Items[radGridClickedRowIndex].Edit = true;
                RadGrid1.Rebind();
                break;
            case "Add":
                RadGrid1.MasterTableView.IsItemInserted = true;
                RadGrid1.Rebind();
                break;
            case "Delete":
                int index1 = Convert.ToInt16(radGridClickedRowIndex);
            if (index1== 0)
            {
                RadGrid1.MasterTableView.PerformDelete(RadGrid1.Items[radGridClickedRowIndex]);
            }
            else
            {
                Response.Write("<script>alert('Please upload pdf files only');</script>");
            }
            break;
        }
    }
    //Performs validation of UnitPrice and UnitsInStock values.
    protected void RadGrid1_UpdateCommand(object sender, GridCommandEventArgs e)
    {
        GridTextColumnEditor gridTextColumnEditor_UnitPrice = (e.Item as GridDataItem).EditManager.GetColumnEditor("UnitPrice") as GridTextColumnEditor;
        GridTextColumnEditor gridTextColumnEditor_UnitsInStock = (e.Item as GridDataItem).EditManager.GetColumnEditor("UnitsInStock") as GridTextColumnEditor;
        if (gridTextColumnEditor_UnitsInStock.Text == String.Empty)
        {
            e.Canceled = true;
            SetDefaultValues(gridTextColumnEditor_UnitsInStock, "0");
        }
        if (gridTextColumnEditor_UnitPrice.Text == String.Empty)
        {
            e.Canceled = true;
            SetDefaultValues(gridTextColumnEditor_UnitPrice, "0");
        }
        else if (gridTextColumnEditor_UnitPrice.Text.Split('.')[0].Length <= 0 || gridTextColumnEditor_UnitPrice.Text.Split('.')[0].Length > 3)
        {
            e.Canceled = true;
            SetDefaultValues(gridTextColumnEditor_UnitPrice, "0");
        }
    }
    private void SetDefaultValues(GridTextColumnEditor gridTextColumnEditor, string text)
    {
        gridTextColumnEditor.Text = text;
    }

    Thanks,
    Shinu.
  4. anup balakrishnan
    anup balakrishnan avatar
    1 posts
    Member since:
    May 2006

    Posted 09 Aug 2018 Link to this post

    I have a radgrid with contextmenu.Contextmenu is loading on rightclick of radgrid row.I want to show different context menu in each radgrid row.I mean i want to conditionally hide few items from Radmenu in each radgrid row.How can this be achieved.
  5. Marin Bratanov
    Admin
    Marin Bratanov avatar
    4533 posts

    Posted 10 Aug 2018 Link to this post

    Hello Anup,

    I have just answered your support ticket with the same question and I am adding the same information and example here for anyone else having a similar situation.

    You can hide, show or otherwise modify menu items on the client and so you can use the menu items API in the click event that shows the menu and, depending on your logic, modify the menu. I am attaching here a basic example that shows how you can do this.

    Regards,
    Marin Bratanov
    Progress Telerik
    Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Back to Top