Using an ASHX generic handler with C# in a classic web form app on a master page to render a report via a ajax call

So I am currently trying to figure out how to use AJAX, JQUERY, BLOCKUI and ASHX handlers to generate a very large report in CSV.  The report generation is spawned by clicking on a hyperlink which will call a javascript function that will execute a AJAX call.  As of right now I get the ajax call to execute, but I have not yet figured out how to get the ajax success method to dump the report to the calling client browser yet.

So this is the question I posted on ASP.NET… If I get an answer that works I will update this post with that info and a link to a code example set.

The QUESTION:

How do I get the ashx handler to work with the master page to generate a report when the user clicks on the hyperlink.

I can get it to work if I just route the hyper link control directly to the ashx page, but I eventually need to add code to block the UI while this report being generated.  In the ajax, I am thinking I need to do some type of action to force the browser to kick out the report.. Just not sure what…

I have a master page with the following code:

<script type="text/javascript">
       function startReport(result) {
            $.ajax({
                url: "HTTPHandlers/TagExportHandler.ashx",
                context: document.body,
                success: function(){
                    alert("done");
                    //Eventually put code to block UI and etc while report is rendering...
                }
            });
        }

</script>

In the markup I have a asp:hyperlink control…

  <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/HTTPHandlers/TagExportHandler.ashx" OnClick="startReport('');">Tag Export 2</asp:HyperLink>

Then I have a ASHX control that does the following..

   public void ProcessRequest(HttpContext context)
        {
            using (var db = new TIPWebITDataContext())
            {
                string _exportContent = "";
                int[] userDepartments = ((User)HttpContext.Current.Session["currentUser"]).Departments.ToArray();

                List<TagDetailsDepartments> tagsModelCsvDepartmentses = null;
                List<TagDetailsNonDepartments> tagModelCsvNonDepartmentses = null;

                var data = GetTagDetails(db, "");
                CsvContext cc = new CsvContext();
                using (var writer = new System.IO.StreamWriter(context.Response.OutputStream))
                {
                    CsvFileDescription output = new CsvFileDescription
                    {
                        QuoteAllFields = true,
                        EnforceCsvColumnAttribute = true
                    };

                    context.Response.AddHeader("content-disposition", "attachment; filename=TagExport.csv");
                    context.Response.ContentType = "text/csv";

                    if (userDepartments.Count() > 1)
                    {
                        tagsModelCsvDepartmentses = ((IQueryable<TagDetailsDepartments>)data).ToList();
                        cc.Write(tagsModelCsvDepartmentses.ToList(), writer, output);
                    }
                    else
                    {
                        tagModelCsvNonDepartmentses = ((IQueryable<TagDetailsNonDepartments>)data).ToList();
                        cc.Write(tagModelCsvNonDepartmentses.ToList(), writer, output);
                    }
                    context.Response.StatusCode = 200;
                    cc = null;

                    writer.Flush();
                    output = null;
                }

                data = null;


                tagsModelCsvDepartmentses = null;
                tagModelCsvNonDepartmentses = null;

                userDepartments = null;
                // context.Session.Add("TagExport", "Complete");
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
        }

Warren

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s