asp.net AJAX Advance Tips & Tricks (10)

Source: Internet
Author: User

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
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.