只能在執行 Render() 的過程中調用 RegisterForEventValidation:十一的部落:Xuite日誌
  • yan.kee
  • 文章分類
  • 最愛連結
  • 我的發燒文
  • 累積 | 今日
    loading......
  • 參觀人氣統計
  • BLOG玩意兒
  • 搜尋文章
  • 關鍵字
  • 最新文章
  • 最新回應
  • Re:只能在執行 Render() 的過程中調用 RegisterForEventValidation,by(11)於2008-07-03
    Re:[轉]自動清除7天以上的檔案 ,by(haoming)於2008-06-13
    Re:[kuso]尿布舞,by(11)於2008-05-18
    Re:[kuso]尿布舞,by(seth)於2008-05-18
    Re:C#中產生GUID,by(yankee)於2008-02-17
    Re:C#中產生GUID,by(jimmy)於2008-02-16
    Re:開發excel 問題討論串,by(11)於2008-02-02
    Re:Connection Strings 討論串,by(11)於2008-01-27
    Re:SQL 分頁,by(11)於2008-01-25
    Re:開發excel 問題討論串,by(11)於2008-01-16
  • 活動
  • 到台灣玩不帶走一張發票
    ===訪客來自===
    Locations of visitors to this page
  • 日誌使用資源






  • 如何使用RSS
    Powered by Xuite
    2008-05-20 11:06 只能在執行 Render() 的過程中調用 RegisterForEventValidation
  • ?
  • .net文章
  • 好文轉寄
  • 平均分數:0 顆星    投票人數:0
    我要評分:
    標籤 : 


    當用GridView導出Execl的時候,會發生只能在執行 Render() 的過程中調用 RegisterForEventValidation的錯誤提示。
    有兩種方法可以解決以上問題:
    1.修改web.config(不推薦)<pages enableEventValidation ="false" ></pages>
    2.直接在導出Execl的頁面修改

    <%@ Page Language="C#" EnableEventValidation = "false" AutoEventWireup="true"

     CodeFile="ExportGridView.aspx.cs" Inherits="ExportGridView" %>

    附:Gridview導出Execl可分頁全部導出代碼(C#)
    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Data.SqlClient;
    using System.IO;

    public partial class EXportTest : System.Web.UI.Page
    {
        
    protected void Page_Load(object sender, EventArgs e)
        
    {
            
    if (!Page.IsPostBack)
            
    {
                BindData(); 
            }


        }

        
        
    private string ConnectionString
        
    {

            
    get return @"Server=localhost;Database=Northwind;
            Trusted_Connection=true
    "; }


        }


        

        
    private void BindData()
        
    {
            
    // make the query 
            string query = "SELECT * FROM Categories";
            SqlConnection myConnection 
    = new SqlConnection(ConnectionString);
            SqlDataAdapter ad 
    = new SqlDataAdapter(query, myConnection);
            DataSet ds 
    = new DataSet();
            ad.Fill(ds, 
    "Categories");
            GridView1.DataSource 
    = ds;
            GridView1.DataBind(); 

            
    /* It's a good idea if you can Cache the DataSet */

        }


        
    public override void VerifyRenderingInServerForm(Control control)
        
    {

            
    // Confirms that an HtmlForm control is rendered for
            the specified ASP.NET server control at run time.

        }


        
    protected void Button1_Click(object sender, EventArgs e)
        
    {
            Response.Clear();

            Response.AddHeader(
    "content-disposition",
            
    "attachment;filename=FileName.xls");

            Response.Charset 
    = "";

            
    // If you want the option to open the Excel file without saving than

            
    // comment out the line below

            
    // Response.Cache.SetCacheability(HttpCacheability.NoCache);

            Response.ContentType 
    = "application/vnd.xls";

            System.IO.StringWriter stringWrite 
    = new System.IO.StringWriter();

            System.Web.UI.HtmlTextWriter htmlWrite 
    =
            
    new HtmlTextWriter(stringWrite);

            
    // turn off paging 
            GridView1.AllowPaging = false;
            BindData(); 
            

            GridView1.RenderControl(htmlWrite);

            Response.Write(stringWrite.ToString());

            Response.End();

            
    // turn the paging on again 
            GridView1.AllowPaging = true;
            BindData();
            
        }

       
        
    protected void GridView1_PageIndexChanging(object sender,
        GridViewPageEventArgs e)
        
    {
            GridView1.PageIndex 
    = e.NewPageIndex;
            BindData(); 
        }

    }

    VB.net:(GridView直接幫定DataSource控件)
    Imports Microsoft.VisualBasic
    Imports System
    Imports System.IO


    Partial 
    Class ManageContents
        
    Inherits System.Web.UI.Page

        
    Protected Sub Page_Load(ByVal sender As ObjectByVal e As System.EventArgs) Handles Me.Load
            
    'If Not Me.IsPostBack Then
            '    If Request.QueryString("username") <> "xucanzhao" Then
            '        Response.Redirect("default.aspx?returnUrl=nousername")
            '    End If
            'End If
            ' ClientScript.RegisterStartupScript(Me.GetType(), "haha", "<script>alert('haha');</script>")
        End Sub


        
    Protected Sub btnExportExcel_Click(ByVal sender As ObjectByVal e As System.EventArgs) Handles btnExportExcel.Click
            Response.Clear()
            Response.Buffer 
    = True

            Response.Charset 
    = "utf-8"

            Response.AddHeader(
    "Content-Disposition""attachment;filename=FileFlow.xls")
            Response.ContentType 
    = "application/ms-excel"
            
    Dim strWriter As New StringWriter()
            
    Dim htw As New HtmlTextWriter(strWriter)
            GridView1.AllowPaging 
    = False
            GridView1.DataBind()
            GridView1.RenderControl(htw)
            Response.Write(strWriter.ToString)
            Response.End()
            GridView1.AllowPaging 
    = True
            GridView1.DataBind()