Click here to Skip to main content
Click here to Skip to main content

HttpContext modification breaks SharePoint Site

By , 19 Dec 2010
 
Often it is required to return a file from a web-part OnSubmit(). The standard practise is:
 
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader("content-disposition", String.Format("attachment; filename={0}",<code>_ExcelFile</code>));
HttpContext.Current.Response.Charset = "";
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
HttpContext.Current.Response.ContentType = "application/ms-excel";
 
However, once the user clicks on the Submit, the SharePoint Site will stop responding. Now there are ways to fix this, either you do a Server Redirect and create a new HttpContext or just add the following line to your Button:
 
btnSubmit.OnClientClick = "this.form.onsubmit = function() {return true;}";
 
So in your WebPart btnSubmit_OnClick(),
 
private void btnSubmit_Click(object sender, EventArgs e)
        {
            if (IsEditMode) return;
            EnsureChildControls();
            System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
            try
            {
                if (dtStartDate.IsDateEmpty || dtEndDate.IsDateEmpty) return;
                String strCashType = ddCashType.Text;
                String strTType = ddTransactionType.Text;
                String xlsFileName = DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls";
                if (ExcelFile == null || ExcelFile == " ")
                    _ExcelFile = "Export" + xlsFileName;
                else
                    _ExcelFile += xlsFileName;
                //
                HttpContext.Current.Response.Clear();
                HttpContext.Current.Response.AddHeader("content-disposition", String.Format("attachment; filename={0}",_ExcelFile));
                HttpContext.Current.Response.Charset = " ";
                HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
                HttpContext.Current.Response.ContentType = "application/ms-excel";
                //

                DataGrid dgTemp = new DataGrid();
                dgTemp.DataSource = oSycoDB.getSycoData(dtStartDate.SelectedDate.ToString("yyyy-MM-dd"),
                                                        dtEndDate.SelectedDate.ToString("yyyy-MM-dd"),
                                                        strCashType, strTType);
                dgTemp.DataBind();
                dgTemp.RenderControl(oHtmlTextWriter);
                HttpContext.Current.Response.Write(oStringWriter.ToString());
                HttpContext.Current.Response.Flush();
                HttpContext.Current.Response.End();
            }
            catch (Exception ex)
            {
                //
                HttpContext.Current.Response.Clear();
                HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=Error.txt");
                HttpContext.Current.Response.Charset = " ";
                HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
                HttpContext.Current.Response.ContentType = "text/plain";
                oHtmlTextWriter.Write(ex.Message+"\n"+ex.StackTrace);
                HttpContext.Current.Response.Write(oStringWriter.ToString());
                HttpContext.Current.Response.Flush();
                HttpContext.Current.Response.End();
                //
                SingleJunket.LogMessage(ex.Message, EventLogEntryType.Error,1002, 1);
            }
        }
 
Enjoy coding.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Adhish Pendharkar
Architect AdhiMarg Technologies Ltd.
Hong Kong Hong Kong
Member
Resident of Hong Kong, TOGAF 8 certified architect with over 15 years of international experience in the IT industry. Exceptional exposure to the marine / shipping technologies and knowledge of large scale database system architecture, enterprise systems design and work flow implementation.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralThank a you a lotmemberMember 32237311 Feb '11 - 10:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130516.1 | Last Updated 19 Dec 2010
Article Copyright 2010 by Adhish Pendharkar
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid