Question : problem exportting gridview to excel spreadsheet

I am attempting, in – visual studio 2008, to execute a subprocedure to export a gridview to an excel spreadsheet.  I get the error:

Control 'GridView1' of type 'GridView' must be placed inside a form tag with runat=server.
Line 304:        Dim stringWrite As New System.IO.StringWriter()
Line 305:        Dim htmlWrite As New HtmlTextWriter(stringWrite)
Line 306:        GridView1.RenderControl(htmlWrite)  ‘ LINE MARKED AS ERROR
Line 307:        Response.Write(stringWrite.ToString())
Line 308:        Response.End()

The markup includes:
<head runat="server">
<form id="form1" runat="server">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
CellPadding="2" SkinID="gridviewSkin" Width="950px"></asp:GridView>

So that there is “runat=server” attribute for the form tag.

The vb includes

Imports System.IO

Dim tw As New StringWriter()
        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"
        Dim stringWrite As New System.IO.StringWriter()
        Dim htmlWrite As New HtmlTextWriter(stringWrite)
        GridView1.RenderControl(htmlWrite) ‘ LINE MARKED AS ERROR

Please resolve the problem.


Answer : problem exportting gridview to excel spreadsheet

You must be using some <%# %> or <%= %> tag or controls like textbox etc in the GridView. Include this code in the page. You need not to write code in the function. You need to add the blank overridden functiononly. I am giving you the C# code, just change it to VB for you.

#region VerifyRenderingInServerForm
    /// <summary>
    /// This is function only used for the Export to Excel function in Master Pages when using the Javascript client side validation using '<%= %> '
    /// </summary>
    /// <param name="control"></param>
    public override void VerifyRenderingInServerForm(Control control)

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


Hope it helps you.
Random Solutions  
programming4us programming4us