This is a migrated thread and some comments may be shown as answers.

Context Menu in radgrid from server side

4 Answers 208 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Dorababu
Top achievements
Rank 1
Dorababu asked on 07 Jul 2012, 01:04 PM
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

4 Answers, 1 is accepted

Sort by
0
Dorababu
Top achievements
Rank 1
answered on 07 Jul 2012, 01:26 PM
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;
0
Shinu
Top achievements
Rank 2
answered on 09 Jul 2012, 05:59 AM
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.
0
anup balakrishnan
Top achievements
Rank 1
answered on 09 Aug 2018, 10:20 AM
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.
0
Marin Bratanov
Telerik team
answered on 10 Aug 2018, 09:43 AM
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.
Tags
Grid
Asked by
Dorababu
Top achievements
Rank 1
Answers by
Dorababu
Top achievements
Rank 1
Shinu
Top achievements
Rank 2
anup balakrishnan
Top achievements
Rank 1
Marin Bratanov
Telerik team
Share this question
or