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

Response.BinaryWrite issue

3 Answers 230 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Alessio S.
Top achievements
Rank 1
Alessio S. asked on 08 Apr 2011, 10:59 AM
Hi,
I've created a custom module with a RadGrid in the backed to manage the module's items. I have a LinkButton inside a RadGrid GridTemplateColumn which I use to send a PDF document from a database to the user. I ran into the issue described in this thread:
http://www.telerik.com/community/forums/aspnet/ajax/radgrid-response-binarywrite-asp-net-ajax.aspx
I used the solution suggested in that thread adding an event like this one to the grid page:

protected void RadGrid1_ItemCreated(object sender, Telerik.WebControls.GridItemEventArgs e)
    {
        if (e.Item is GridDataItem)
        {
            Button btn = (e.Item as GridDataItem)["TemplateColumn"].FindControl("Button1") as Button;
            ScriptManager1.RegisterPostBackControl(btn);
        }
        if (e.Item is GridCommandItem)
        {
            Button btncmd = (e.Item as GridCommandItem).FindControl("Button2") as Button;
            ScriptManager1.RegisterPostBackControl(btncmd);
        }
    }

This seemed to fix the problem, but then I noticed if I make the grid sortable and I click on a column header to sort the items, then I click on any item's LinkButton, I get again the Sys.WebForms.PageRequestManagerParserErrorException. Same happens if I enable grouping and group the items by any column.

This is the LinkButton codebehind:

protected void ItemsGrid_ItemCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
    {
        if (e.Item.ItemType == GridItemType.Item || e.Item.ItemType == GridItemType.AlternatingItem)
        {
            Guid candidaturaId = new Guid(ItemsGrid.MasterTableView.DataKeyValues[e.Item.ItemIndex]["ID"].ToString());
 
        // create a new instance of ContactsManager with default provider
        CandidatureManager cm = new CandidatureManager();
 
        // get the desired candidatura and set the values of the form
        ICandidatura candidatura = cm.GetCandidatura(candidaturaId);
 
            string ext = Path.GetExtension(candidatura.AllegatoFilename).ToLower();
            CustomDownload cd =
                new CustomDownload
                {
                    Filename = candidatura.AllegatoFilename,
                    MimeType = (ext == ".pdf") ? MediaTypeNames.Application.Pdf : "application/unknown",
                    Data = candidatura.Allegato.ToArray(),
                };
 
            ScriptManager sm = (ScriptManager)Page.FindControl("TheScriptManager");
 
            Response.ClearHeaders();
            Response.ClearContent();
            Response.AddHeader("Accept-Ranges", cd.Data.Length.ToString());
            Response.AddHeader("Content-Disposition", "attachment;filename=" + cd.Filename);
            Response.ContentType = cd.MimeType;
            if (cd.Data.Length > 0)
                Response.BinaryWrite(cd.Data);
            Response.End();
        }
    }


Thank you,
Alessio Scalerandi

3 Answers, 1 is accepted

Sort by
0
Iana Tsolova
Telerik team
answered on 13 Apr 2011, 11:51 AM
Hello Alessio S.,

What if you call the code in the ItemCommand event handler when specific command is fired, not on each command? For instanbce when e.CommandName is equal to the command name of the btncmd button.

All the best,
Iana
the Telerik team

Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.

0
Alessio S.
Top achievements
Rank 1
answered on 14 Apr 2011, 08:34 AM
At the moment the only command fired by the grid items is the one to download the file, there are no delete/edit buttons. However I've tried adding a check on CommandName and it doesn't fix the problem.
0
Iana Tsolova
Telerik team
answered on 18 Apr 2011, 01:44 PM
Hello,

Note that when you sort, page or filter the grid, the ItemCommand event is fired as well. That is why I assume the workaround stopped working after you enabled sorting.

For more information on RadGrid events sequence check this article.

However, can you elaborate on how is the grid ajaxified? Is it wrapped in RadAjaxPanel/ASP:UpdatePanel or you are using RadAjaxManager? If you are using RadAjaxManager, then you should use the second approach from this article for disabling ajax instead.

All the best,
Iana
the Telerik team

Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.

Tags
General Discussions
Asked by
Alessio S.
Top achievements
Rank 1
Answers by
Iana Tsolova
Telerik team
Alessio S.
Top achievements
Rank 1
Share this question
or