lunes, 5 de mayo de 2014

Error El control 'GridView1' debe colocarse dentro de una etiqueta de formulario con runat=server.

La solución es colocar en nuestro fuente el codigo VerifyRenderingInServerForm del siguiente modo:


protected void Page_Load(object sender, EventArgs e){
...
}

public override void VerifyRenderingInServerForm(Control control)
{
  /* Confirms that an HtmlForm control is rendered for the specified ASP.NET
     server control at run time. */
}



Veamos mas detalles para aclararlo


Fuente0: http://stackoverflow.com/questions/6343630/gridview-must-be-placed-inside-a-form-tag-with-runat-server-even-after-the-gri


You are calling GridView.RenderControl(htmlTextWriter), hence the page raises an exception that a Server-Control was rendered outside of a Form.
You could avoid this execption by overriding VerifyRenderingInServerForm
public override void VerifyRenderingInServerForm(Control control)
{
  /* Confirms that an HtmlForm control is rendered for the specified ASP.NET
     server control at run time. */
}
See here and here.


Fuente1: http://aspalliance.com/771_CodeSnip_Exporting_GridView_to_Excel


In this code snippet, I will show how you can export data from a GridView control to a Microsoft Excel spreadsheet.
The Code
Listing 1: Default.aspx
<form id="form1" runat="server">
  <div>
  <asp:GridView ID="GridView1" runat="server">
  </asp:GridView>
  </div>
  <br />
  <asp:Button ID="BtnExport" runat="server" OnClick="BtnExport_Click"
  Text="Export to Excel" />
</form>
Listing 2: Default.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
  if (!Page.IsPostBack)
  {
    BindData();
  }
}
 
private void BindData()
{
  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();
}
 
private string ConnectionString
{
  get { return @"Server=localhost;Database=NorthWind;Trusted_Connection=true"; }
 
}
protected void BtnExport_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 then
  // 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);
  GridView1.RenderControl(htmlWrite);
  Response.Write(stringWrite.ToString());
  Response.End();
}
 
In the above listing, the GridView control will be populated with the data from the Categories table of the Northwind database. You will have to give the appropriate Server name or IP instead of localhost as server name in the above connection string.
If you use this code and try to export the GridView control, you will see an error message saying that the GridView control must be placed inside the form tags with the runat = server attribute.
This is pretty confusing, since your GridView is already inside the form tags and also contains the runat = server attribute. You can easily resolve this error by adding the following lines.
Listing 3: Overiding VerifyRenderingInServerForm Method
public override void VerifyRenderingInServerForm(Control control)
{
  /* Confirms that an HtmlForm control is rendered for the specified ASP.NET
     server control at run time. */
}
Yup, that’s it. Now, when you click the button, the GridView control will be exported correctly. It will prompt you either to open the file as it is or to save it elsewhere.

Fuente2: http://forums.asp.net/t/1016960.aspx?Problem%20with%20GridView%20s%20RenderControl

7 comentarios:

  1. De nada William. Gracias por tu visita y tu comentario para saber que te ha ayudado. Hasta pronto.

    ResponderEliminar
  2. Muchas gracias, fue de bastante ayuda. :)

    ResponderEliminar
  3. Me alegro Eduardo, saludos. Gracias por apoyar el Blog

    ResponderEliminar
  4. Respuestas
    1. me alegro que te ahorrara tiempo y quebraderos de cabeza, xd. Saludos

      Eliminar
  5. Solo me imprime una etqieuat div, ayudenme por favor :(

    ResponderEliminar

Jesús Moreno - Ingeniero Ténico Informático - consultor Informático

Hola, soy Jesús Moreno Ingeniero Técnico Informático en sistemas por la US y propietario de éste blog. Mi trabajo en los ultimos años se ...