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

Custom field in the GridContextMenu. Pass the selected item's path to the server when the field is clicked.

HOW-TO

Custom field in the GridContextMenu. Pass the selected item's path to the server when the field is clicked.

DESCRIPTIONs

Add custom field in the GridContextMenu and implement the server functionality when that field is clicked. The problem is passing the path of the selected item to the server. The RadFileExplorer control does not have server property that holds the currently selected file's path. The CurrentFolderproperty is similar, but it holds the path to the currently selected directory.
We need to use some client-side and server-side scripts in order to achieve the desired result.

SOLUTION

The first approach

  1. Create a RadMenuItem and add it to the GridContextMenu'sitem collection :

       RadMenuItem item = new RadMenuItem("UnZip");  
    item.PostBack = false;  
    item.Value = "CustomValue";  
    RadFileExplorer1.GridContextMenu.Items.Add(item); 
    
       Dim item As New RadMenuItem("UnZip")  
    item.PostBack = False 
    item.Value = "CustomValue" 
    RadFileExplorer1.GridContextMenu.Items.Add(item)  
    
  2. Attach a handler to the GridContextMenu''s OnClientItemClicked event as follows :

````C# :
RadFileExplorer1.GridContextMenu.OnClientItemClicked = "OnGridContextItemClicked";

RadFileExplorer1.GridContextMenu.OnClientItemClicked = "OnGridContextItemClicked" 
  1. Add an RadAjaxPanel to the page. Attach a server handler to the OnAjaxRequestevent. Please note that you can use different approach in this step (for example __doPostBack() function instead of the ajax request).
  2. Implement the OnGridContextItemClickedfunction as follows :

       <script type="text/javascript">  
      function OnGridContextItemClicked(oGridMenu, args)  
      {  
          var menuItemText = args.get_item().get_text();  
          if (menuItemText == "UnZip")  
          {// 'UnZip' command  
              var oExplorer = $find("<%= RadFileExplorer1.ClientID %>");  
              var oAjaxPanel = $find("<%= RadAjaxPanel1.ClientID %>");  
    
              var selectedItem = oExplorer.get_selectedItem();  
              if (selectedItem)  
              {  
                  var itemPath = selectedItem.get_path(); // get the path of the current item  
                  oAjaxPanel.ajaxRequest(itemPath); // Call the RadAjaxPanel1_AjaxRequest function on the server ;  
              }  
              else 
              {  
                  alert("Please, selct an item");  
              }  
          }  
      }  
    </script> 
    

    Inside of this function we found the RadFileExplorer's client-side object and use its API in order to get the selected item. Then we trigger an AJAX request to the server and pass the path to the selected file as a parameter of the ajaxRequest() function.

  3. The AJAX request is handled on the server as follows :

          protected void RadAjaxPanel1_AjaxRequest(object sender, AjaxRequestEventArgs e)
    {
    string path = e.Argument;
    Label1.Text = path;
    }
    </code></pre></div>      <div class='tabbedCode'><pre lang="VB"><code>      Protected Sub RadAjaxPanel1_AjaxRequest(ByVal sender As Object, ByVal e As AjaxRequestEventArgs)  
         Dim path As String = e.Argument  
         Label1.Text = path  
      End Sub 
    

The second approach

In this case, we use an asp:HiddenField and store the selected item's path in its value property :

  1. You need to implement the steps from 1 to 3 of the first described approach
  2. Then implement the OnGridContextItemClickedfunction as follow :
          function OnGridContextItemClicked(oGridMenu, args)
      {
         var menuItemText = args.get_item().get_text();
         if (menuItemText == "UnZip")
         {// 'UnZip' command
            var oExplorer = $find("<%= RadFileExplorer1.ClientID %>");
            var selectedItem = oExplorer.get_selectedItem();
            if (selectedItem)
            {
                  var itemPath = selectedItem.get_path(); // get the path of the current item
                  var oHiddenField = $get("<%= HiddenField1.ClientID %>");
                  oHiddenField.value = itemPath; // Store the path into the hidden field
            }
            else
            {
                  alert("Please, select an item");
            }
            var oAjaxPanel = $find("<%= RadAjaxPanel1.ClientID %>");
            oAjaxPanel.ajaxRequest(); // Call the RadAjaxPanel1_AjaxRequest function on the server ;
         }
      }
    

    1. On the server you can get the value of the hidden field as follow :

        protected void RadAjaxPanel1_AjaxRequest(object sender, AjaxRequestEventArgs e)  
        {  
           // The second approach :  
           string path = HiddenField1.Value;  
           Label1.Text = path;  
        } 
      

          Protected Sub RadAjaxPanel1_AjaxRequest(ByVal sender As Object, ByVal e As AjaxRequestEventArgs)
    ' The second approach :
    Dim path As String = HiddenField1.Value
    Label1.Text = path
    End Sub
In this article