Home » Javascript » onRequestStart javascript function, error on downloading pdf file on radgrid Ajaxifying

onRequestStart javascript function, error on downloading pdf file on radgrid Ajaxifying

Posted by: admin November 1, 2017 Leave a comment

Questions:

I’ve a Radgrid with couple of ImageButton on it. On “Download” imagebutton click I need to open the dialog box for saving or opening the file. Since I’m using RadAjaxManager it’s not opening up the dialog box though it gets through the code. I’m trying the below code to avoid ajaxifying on “Download” button click. I could’t figure out how to pass the args value to Javascript function to disable ajax. it throwing error saying “args” not defined.

 <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server" ClientEvents-OnRequestStart="onRequestStart()">
    <AjaxSettings>
        <telerik:AjaxSetting AjaxControlID="btnUpdateUsers">
            <UpdatedControls>
                <telerik:AjaxUpdatedControl ControlID="radGrid1" LoadingPanelID="RadAjaxLoadingPanel1"></telerik:AjaxUpdatedControl>
            </UpdatedControls>
        </telerik:AjaxSetting>
 </AjaxSettings>
</telerik:RadAjaxManager>

<telerik:RadAjaxLoadingPanel runat="server" ID="RadAjaxLoadingPanel1" Skin="Default"></telerik:RadAjaxLoadingPanel>  

<telerik:RadGrid ID="radGrid1" RenderMode="Lightweight" runat="server"                                 
AllowPaging="true" AllowSorting="True" AutoGenerateColumns="false" 
OnItemCommand="radGrid1_ItemCommand" 
OnItemDataBound="radGrid1_ItemDataBound" 
OnNeedDataSource="radGrid1_NeedDataSource" PageSize="20" 
ShowFooter="true">

<MasterTableView AllowAutomaticDeletes="false" AllowAutomaticInserts="false" 
AllowAutomaticUpdates="false" AllowFilteringByColumn="true" 
CommandItemDisplay="Top" DataKeyNames="ID">

<Columns>
<telerik:GridBoundColumn DataField="ID" DataType="System.Guid" 
 HeaderText="Name" ReadOnly="true" Visible="false" />                               
<telerik:GridButtonColumn ButtonType="ImageButton"  
CommandName="download_file" HeaderText="Download PDF" 
ImageUrl="~/Images/download.png" ItemStyle-HorizontalAlign="Center" 
Text="Download" UniqueName="Download">
</telerik:GridButtonColumn>                                 
</Columns>
</MasterTableView>
</telerik:RadGrid>

  **aspx.cs file**    


 protected void radGrid1_ItemCommand(object sender, GridCommandEventArgs e)
    {
        if (e.CommandName == "download_file")
        {

                GridDataItem ditem = (GridDataItem)e.Item;

                ImageButton imgBtn = (ImageButton)ditem["Download"].Controls[0];
                int index = e.Item.ItemIndex;
                imgBtn.Attributes.Add("onclick", "OnRequestStart('" + e.Item.ItemIndex + "');");

                Response.Clear();
                Response.ClearHeaders();
                Response.AddHeader("Content-Type", "Application/octet-stream");
                Response.AddHeader("Content-Length", d.PDF.Length.ToString());
                Response.AddHeader("Content-Disposition", "attachment; filename=" + string.Format("file_{0}_{1}.PDF", d.User.DisplayName, d.Year));
                Response.BinaryWrite(d.PDF);
              if (Response.IsClientConnected)
                {
                    Response.Flush();
                }

                Response.End();
            }
        }

**Javascript**

function onRequestStart(index)
{         
        var grid = $find("<%=radGrid1.ClientID %>");        
        var masterTable = grid.get_masterTableView();
        var rows = masterTable.get_dataItems();
        var indexnew = rows[index];            

        if (args.get_eventTarget().indexOf(indexnew) >= 0)
            args.set_enableAjax(false);
    }
Answers: