radcontextmenu get which target fired onclick event

1 Answer 23 Views
UI for ASP.NET AJAX in ASP.NET MVC
Charlie
Top achievements
Rank 1
Charlie asked on 04 Oct 2021, 09:09 AM

Hi,

I've been looking for an answer but haven't been anything so apologies if it has already been answered.

I had a RadContextMenu with multiple targets (divs), when the onitemclick event is triggered how do I get which target the click event was launched from?

I need to know which control the contextMenu has been launched from as I'm using it as an edit/delete interface.

 


<telerik:radcontextmenu runat="server" id="ContextMenu1"
                            enableroundedcorners="True" enableshadows="True" onitemclick="RadMenuItem_Click" meta:resourcekey="ContextMenu1Resource1">
                            <targets>
                                <telerik:contextmenuelementtarget elementid="divField1" />
                                <telerik:contextmenuelementtarget elementid="divField2" />
                                <telerik:contextmenuelementtarget elementid="divField3" />
                                <telerik:contextmenuelementtarget elementid="divField4" />
                                <telerik:contextmenuelementtarget elementid="divField5" />
                                <telerik:contextmenuelementtarget elementid="divField6" />
                                <telerik:contextmenuelementtarget elementid="divField7" />
                                <telerik:contextmenuelementtarget elementid="divField8" />
                                <telerik:contextmenuelementtarget elementid="divField9" />
                                <telerik:contextmenuelementtarget elementid="divField10" />
                            </targets>
                            <items>
                                <telerik:radmenuitem text="Edit" value="Edit" runat="server" meta:resourcekey="RadMenuItemResource1" />
                                <telerik:radmenuitem text="Delete" value="Delete" runat="server" meta:resourcekey="RadMenuItemResource2" />
                            </items>
                        </telerik:radcontextmenu>

 

Thanks in advance

1 Answer, 1 is accepted

Sort by
0
Vessy
Telerik team
answered on 07 Oct 2021, 05:49 PM

Hello Charlie,

Your assumption is correct - the OnItemClick event does not provide information about the target element of the ContextMenu. What you can to is to get the ID of the target element in the OnClientShown event of the control, keep its value into a hidden field and access is later in the OnItemClick event handler like follows:

       <div id="divField1">div 1</div>
        <div id="divField2">div 2</div>
        <div id="divField3">div 3</div>
        <div id="divField4">div 4</div>
        <div id="divField5">div 5</div>
        <div id="divField6">div 6</div>
        <div id="divField7">div 7</div>
        <div id="divField8">div 8</div>
        <div id="divField9">div 9</div>
        <div id="divField10">div 10</div>


        <telerik:RadContextMenu runat="server" ID="ContextMenu1"
            EnableRoundedCorners="True" EnableShadows="True" OnClientShown="onClientShown"
            OnItemClick="ContextMenu1_ItemClick" meta:resourcekey="ContextMenu1Resource1">
            <Targets>
                <telerik:ContextMenuElementTarget ElementID="divField1" />
                <telerik:ContextMenuElementTarget ElementID="divField2" />
                <telerik:ContextMenuElementTarget ElementID="divField3" />
                <telerik:ContextMenuElementTarget ElementID="divField4" />
                <telerik:ContextMenuElementTarget ElementID="divField5" />
                <telerik:ContextMenuElementTarget ElementID="divField6" />
                <telerik:ContextMenuElementTarget ElementID="divField7" />
                <telerik:ContextMenuElementTarget ElementID="divField8" />
                <telerik:ContextMenuElementTarget ElementID="divField9" />
                <telerik:ContextMenuElementTarget ElementID="divField10" />
            </Targets>
            <Items>
                <telerik:RadMenuItem Text="Edit" Value="Edit" runat="server" meta:resourcekey="RadMenuItemResource1" />
                <telerik:RadMenuItem Text="Delete" Value="Delete" runat="server" meta:resourcekey="RadMenuItemResource2" />
            </Items>
        </telerik:RadContextMenu>
        <asp:HiddenField ID="targetElement" runat="server" />
        <script>
            function onClientShown(sender, e) {
                $get("<%=targetElement.ClientID%>").value = e.get_targetElement().id;
            }
        </script>

    protected void ContextMenu1_ItemClick(object sender, Telerik.Web.UI.RadMenuEventArgs e)
    {
        Response.Write("Server event raised -- you clicked: " + targetElement.Value);
    }

 

Regards,
Vessy
Progress Telerik

Remote troubleshooting is now easier with Telerik Fiddler Jam. Get the full context to end-users' issues in just three steps! Start your trial here - https://www.telerik.com/fiddler-jam.
Tags
UI for ASP.NET AJAX in ASP.NET MVC
Asked by
Charlie
Top achievements
Rank 1
Answers by
Vessy
Telerik team
Share this question
or