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

Tagged as

HttpContext modification breaks SharePoint Site

, 19 Dec 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
Modifying HttpContext context-disposition in a WebPart will break SharePoint Response
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)

Share

About the Author

Adhish Pendharkar
Architect AdhiMarg Technologies Ltd.
Hong Kong Hong Kong
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.

Comments and Discussions

 
GeneralThank a you a lot PinmemberMember 32237311-Feb-11 11:04 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

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