RadContextMenu for multiple controls, how to know which one call it

2 posts, 0 answers
  1. reguapo
    reguapo avatar
    16 posts
    Member since:
    Jul 2009

    Posted 11 Jul 2013 Link to this post

    I have multiple RadListBox that basically are doing the same and has more likely the same behavior except maybe for the data, so I want to use the same RadContextMenu for all of them but on the OnItemClick(server side) I need to know which of those controls call it, How can I approach this?
    Thanks in advance
  2. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 12 Jul 2013 Link to this post

    Hello Reguapo,

    Try the following markup,JS and C# to achieve your scenario
    aspx:
    <telerik:RadListBox ID="RadListBox1" runat="server">
        <Items>
            <telerik:RadListBoxItem Text="001" />
            <telerik:RadListBoxItem Text="002" />
            <telerik:RadListBoxItem Text="003" />
            <telerik:RadListBoxItem Text="004" />
            <telerik:RadListBoxItem Text="005" />
        </Items>
    </telerik:RadListBox>
    <br />
    <br />
    <telerik:RadListBox ID="RadListBox2" runat="server">
        <Items>
            <telerik:RadListBoxItem Text="001" />
            <telerik:RadListBoxItem Text="002" />
            <telerik:RadListBoxItem Text="003" />
            <telerik:RadListBoxItem Text="004" />
            <telerik:RadListBoxItem Text="005" />
        </Items>
    </telerik:RadListBox>
    <telerik:RadContextMenu ID="RadContextMenu1" runat="server" OnClientItemClicked="OnClientItemClicked"
        OnItemClick="RadContextMenu1_ItemClick">
        <Targets>
            <telerik:ContextMenuElementTarget ElementID="RadListBox1" />
            <telerik:ContextMenuElementTarget ElementID="RadListBox2" />
        </Targets>
        <Items>
            <telerik:RadMenuItem Text="1001" />
            <telerik:RadMenuItem Text="1002" />
            <telerik:RadMenuItem Text="1003" />
        </Items>
    </telerik:RadContextMenu>
    <asp:HiddenField ID="ListID" runat="server" />
    JS:
    <script type="text/javascript">
        function OnClientItemClicked(sender, args) {
     
            var ctrlID = document.getElementById("ListID");
            var listbox = args._targetElement.id;
            if (listbox.indexOf("RadListBox1") == 0) {
     
                ctrlID.value = "RadListBox1";
            }
            else if (listbox.indexOf("RadListBox2") == 0) {
     
                ctrlID.value = "RadListBox2";
            }
            else {
            }
        }
    </script>
    C#:
    protected void RadContextMenu1_ItemClick(object sender, Telerik.Web.UI.RadMenuEventArgs e)
      {
          if (ListID.Value == "RadListBox1")
          {
     
          }
          else if (ListID.Value == "RadListBox2")
          {
     
          }
      }

    Thanks
    Shinu.
  3. UI for ASP.NET Ajax is Ready for VS 2017
Back to Top