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();
}