2009-11-03 5 views
0

travail sur asp.net vs05.when i exporter gridview d'exceller que je reçois l'erreur ci-dessousGridView excel

RegisterForEventValidation ne peut être appelé lors de rendu();

pourquoi je reçois cette erreur .Comment la résoudre?

public void ToExcel() 
{ 
    string attachment = "attachment; filename=Employee.xls"; 
      Response.ClearContent(); 
      Response.AddHeader("content-disposition", attachment); 
      Response.ContentType = "application/ms-excel"; 
      StringWriter stw = new StringWriter(); 
      HtmlTextWriter htextw = new HtmlTextWriter(stw); 
      gvSearch.RenderControl(htextw); 
      Response.Write(stw.ToString()); 
      Response.End(); 
} 


public override void VerifyRenderingInServerForm(Control control) 
    { 

    } 

Répondre

0

// - ajouté pour gérer les caractères spéciaux dans Excel

Response.ContentEncoding = Encoding.Unicode; 
        Response.BinaryWrite(Encoding.Unicode.GetPreamble()); 

        gvSearch.EnableViewState = false; 

// - ajouté pour gérer les caractères spéciaux dans Excel

Ajoutez ces lignes et voir si ça aide.

StringWriter stw = new StringWriter(); 
      HtmlTextWriter htextw = new HtmlTextWriter(stw); 
      gvSearch.RenderControl(htextw); 
      Response.Write(stw.ToString()); 
      Response.End(); 
} 

C'est une photo dans l'obscurité.

+0

Je reçois toujours cette erreur "RegisterForEventValidation ne peut être appelé que pendant Render();" – HelloBD

0

dans la page aspx, Remplacez la suivante par false: AllowPaging = "False" AllowSorting = "False" et supprimez Pagesize = "10".