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

How to download a file on RadContentMenuItem Click

2 Answers 231 Views
Menu
This is a migrated thread and some comments may be shown as answers.
K
Top achievements
Rank 1
K asked on 14 Jul 2016, 08:12 PM

I want to call a function that writes a file for download from a RadContextMenu.  The function works fine when I call it from a button click. But I can't call it from the server side OnContextMenuItemClick because the Response.End() call doesn't let the menu click finish (the load icon just keeps going).  So I am trying to intercept the menu click on the client side, cancel the event, and post the button click instead.  My breakpoint on javascript _doPostBack() gets hit,  my breakpoint on Page_Load gets hit, but my breakpoint on ButtonRptRpt_Click function does not.  Why not?

How can I download a file as a result of a RadMenuItem click? (Opening another browser tab is not an option.)

<telerik:RadTreeView ID="TreeViewBatchRules" runat="server" CheckBoxes="True"
    MultipleSelect="True" ToolTip="Right-click on nodes to see rule batch run menu."
    OnClientContextMenuItemClicking="TreeViewBatchRules_ClientContextMenuItemClicking"
    OnContextMenuItemClick="TreeViewBatchRules_ContextMenuItemClick">
    <DataBindings>
      <telerik:RadTreeNodeBinding Expanded="False" />
    </DataBindings>
    <ContextMenus>
      <telerik:RadTreeViewContextMenu ID="TreeViewBatchRulesContextMenu" runat="server">
        <Items>
          <telerik:RadMenuItem Value="Thing1" Text="Thing 1">    
             </telerik:RadMenuItem>
             <telerik:RadMenuItem Value="CreateReport" Text="Create Report">
             </telerik:RadMenuItem>
        </Items>
      </telerik:RadTreeViewContextMenu>
    </ContextMenus>
  </telerik:RadTreeView>
   
<!-- works fine on button click --> 
<asp:Button ID="ButtonRunRpt" runat="server" Text="Run Report" OnClick="ButtonRunRpt_Click" />

function TreeViewBatchRules_ClientContextMenuItemClicking(sender, eventArgs) { 
    var node = eventArgs.get_node();
    var item = eventArgs.get_menuItem();
    var menu = item.get_menu();   
 
    if (item.get_text() == "Create Report"){
        eventArgs.set_cancel(true);
        var ruleId = node.get_value();
 
        // the postback gets called but doesn't "arrive" at the ButtonRunRpt_Click
        var btnId = "<%=ButtonRunRpt.ClientID %>"
        __doPostBack(btnId, '');
    }           
}
protected void TreeViewBatchRules_ContextMenuItemClick
                  (object Sender,
                       Telerik.Web.UI.RadTreeViewContextMenuEventArgs e)
{
    int ruleId = int.Parse(TreeViewBatchRules.CheckedNodes[0].Value);
 
    switch (e.MenuItem.Value)
    {
       case "Thing1" : ... break;
       case "CreateReport" : BuildReport(ruleId); break; // load icon won't go away; file doesn't download       
    }
}
 
 
protected void ButtonRunRpt_Click(object Sender,
                                  EventArgs e)
{
    int ruleId = int.Parse(TreeViewBatchRules.CheckedNodes[0].Value);
    BuildReport(nodeId); 
}  
 
private void BuildReport(int nodeId)
{
    var ms = GetReport(ruleId);
     
    HttpContext.Current.Response.Clear();
    HttpContext.Current.Response.Buffer = true;
    HttpContext.Current.Response.ContentType = "application/pdf";  
    HttpContext.Current.Response.AddHeader("Content-disposition", "attachment; filename='myReport.pdf'");     
 
    Response.BinaryWrite(ms);
     
    HttpContext.Current.Response.Close();
    HttpContext.Current.Response.End();
}

2 Answers, 1 is accepted

Sort by
0
K
Top achievements
Rank 1
answered on 14 Jul 2016, 08:26 PM
I posted this as a support ticket once I realized that was the better option for me.  Sorry for the duplication.
0
Nencho
Telerik team
answered on 15 Jul 2016, 12:35 PM
Hello,

I have already replied to your question in the support ticket, which is why, I would ask you to continue the correspondence in the other thread.

Than you!

Regards,
Nencho
Telerik by Progress
Do you need help with upgrading your ASP.NET AJAX, WPF or WinForms projects? Check the Telerik API Analyzer and share your thoughts.
Tags
Menu
Asked by
K
Top achievements
Rank 1
Answers by
K
Top achievements
Rank 1
Nencho
Telerik team
Share this question
or