Click here to Skip to main content
15,908,112 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I am getting error while using render control Unhandle exception it showing i place gridview in form tag only.
C#
XmlTextReader xtr = new XmlTextReader("http://localhost:50197/Trail4/xml/xmltest.xml");
        DataSet ds = new DataSet();
        ds.ReadXml(xtr);
        xtr.Close();
        GridView1.DataSource = ds;
           
        
        GridView1.DataBind();
        Response.ClearContent();
        Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "xml.doc"));
        Response.Charset = "";
        Response.ContentType = "application/ms-word";
        StringWriter sw = new StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);
       GridView1.RenderControl(htw);
      
        Response.Write(sw.ToString());
        Response.End();
Posted
Updated 27-May-12 20:46pm
v2

1 solution

Control 'GridView1' of type 'GridView' must be placed inside a form tag with runat=server
The above exception occurs when one tries to export a GridView control to Word, Excel, PDF, CSV or any other formats.

Reason:
.NET compiler thinks that the control is not added to the form and is rendered.

Solution:
Tell compiler that the control is rendered explicitly by overriding the VerifyRenderingInServerForm event.
Something like:
C#
public override void VerifyRenderingInServerForm(Control control)
{
    /* Verifies that the control is rendered */
}

This Error/issue reported & resolution suggested by MS here: RenderControl doesn't work for GridView[^]
 
Share this answer
 
Comments
jvamsikrishna 28-May-12 3:15am    
Thank You very much for your quick reply
Sandeep Mewara 28-May-12 4:36am    
Welcome.
Kurac1 20-Apr-13 11:36am    
I am new in this Can someone help me

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900