匯出Excel

  • 1298
  • 0

RegisterForEventValidation can only be called during Render(); 只能在執行 Render() 的過程中調用 RegisterForEventValidation

aspx

<%@ Page Language="C#" EnableEventValidation = "false" AutoEventWireup="true" CodeFile="Shedule.aspx.cs" Inherits="Shedule" %>

 protected void btnPrint_Click(object sender, EventArgs e)
    {

        System.IO.StringWriter tw = new System.IO.StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(tw);
        string excelFileName = "abc.xls";

        Context.Response.ContentType = "application/x-excel";
        Context.Response.AddHeader("content-disposition", "attachment;filename=" + Server.UrlEncode(excelFileName));

        //若想要輸出自訂文字,在這邊加,使用html語法即可
        string query = "";
        Context.Response.Write(query);
        //********************************
        this.PlCal.RenderControl(hw);
        Context.Response.Write(tw.ToString());
        Context.Response.End();

  }

public override void VerifyRenderingInServerForm(Control control)
{
//避免「型別 xxx 的控制項 xxx 必須置於有 runat=server 的表單標記之中。」的問題
}