Troubleshoot page export (Word/excel) issues when using AJAX Extender
Objective:
A customer asks this question: we can easily export the ASP.net page to Excel or Word using response. However, if there is an AJAX Control Toolkit Extender on the page, an error occurs (Extender control ' XXX ' isn't a registered Extender control). I searched, asp.net forum There are many such problems can not be resolved, so write this solution, and share with you.
Problem Recurrence:
First, using response to export the ASP.net page to Excel or Word, the code is simpler, as follows:
public void Exportcontrol (System.Web.UI.Control source, DocumentType type,string name)
{
if (type = = Documenttype.excel)
{
Excel
Response.appendheader ("Content-disposition", "Attachment;filename=" +name+ ". xls");
Response.ContentType = "Application/ms-excel";
}
else if (type = = Documenttype.word)
{
Word
Response.appendheader ("Content-disposition", "attachment;filename=" + name + ". Doc");
Response.ContentType = "Application/ms-word";
}
Response.Charset = "UTF-8";
response.contentencoding = System.Text.Encoding.UTF8;
Source. Page.enableviewstate = false;
System.IO.StringWriter writer = new System.IO.StringWriter ();
System.Web.UI.HtmlTextWriter HTMLWriter = new System.Web.UI.HtmlTextWriter (writer);
Source. RenderControl (HTMLWriter);
Response.Write (writer. ToString ());
Response.End ();
}
public enum DocumentType
{
Word
Excel
}