0

Ok so here is the situation.

I have Gridview content being exported to an excel sheet. Basically I have a button that calls a method (e.g. btnExcel_Clicked). The c# code behind is as such

protected void btnExcel_Click ( object sender, EventArgs e )
{
    Response.Clear( );
    Response.AddHeader( "content-disposition", "attachment; filename=" + "test" + "_Registration_Forms.xls" );
    Response.Charset = "";
    Response.ContentType = "application/vnd.ms-excel";
    Page.EnableViewState = false;

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

    clientGridView.RenderControl( htmlWrite ); //this is the name of my gridview

    Response.Write( stringWrite.ToString( ) );
    Response.End( );
}

When the button fires, it then shows the error:

"System.Web.HttpException: Control 'clientGridView' of type 'GridView' must be placed inside a form tag with runat=server."

Indicating that the following code is where the error occurred:

Line 217:        clientGridView.RenderControl( htmlWrite );

Why would this be if my form does indeed have the runat="server" inside it?

I suspect it may be as a result of how I am using the page, the gridview is wrapped in an update panel (not sure if this would cause any problems?) and I have some javascript at the top of the page to allow the page to refresh each search query. Which looks like so:

<script type="text/javascript">
    function RefreshUpdatePanel() {
        __doPostBack('<%= txtClientName.ClientID %>', '');
        $('#totals').fadeIn('slow');
        $('#clientGrid').fadeIn('slow');
        $('#btnReport').fadeIn('slow');
    };
</script>

This basically just fires my update panel everytime I enter a value into txtClientName

I am not sure how any of this would be causing such an error. If someone could shed some light on this or guide me in the right direction I would be greatful. Is this because my gridview is placed within the update panel? arghhhh..

Thanks in advance!

EDIT: ok first problem solved...but a new problem has arisen.

by placing this code in my code behind:

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

I can get past that error, however a new error arose:

System.InvalidOperationException: RegisterForEventValidation can only be called during     Render();

Bah.

Edit 2: Ok problem mostly solved. By entering in EnableEventValidation ="false" in @ Page section, this functions properly. However, Is this now unsafe is there a way around this?

Edit 3: Also, is there a way of achieving this without losing the cell borders in microsoft excel?

jshbrmn
  • 1,737
  • 3
  • 22
  • 52

1 Answers1

1

I know this does not directly anser your rendering problem but if your datagrid is bound to a dataset why not export it directly from the dataset using a method such as the accepted anser in this question:

Export DataTable to Excel File

Update

If you do not have a dataset you can generate one easily using SQLDataAdapter and fill a dataset like so:

var dataAdapter = new SqlDataAdapter("Select * From table",
 "Data Source=MySQLServerName;Initial Catalog=DatabaseName;Integrated Security=True");

        var dataset = new DataSet();

        dataAdapter.Fill(dataset);

Update 2

If you want to use the sqlcommand text from your datasource you can do this:

var dataAdapter = new SqlDataAdapter(mySqlDatasource.SelectCommand,
     "Data Source=MySQLServerName;Initial Catalog=DatabaseName;Integrated Security=True");
Community
  • 1
  • 1
Richard
  • 21,728
  • 13
  • 62
  • 101
  • I'm not too knowledgeable about this (bound to a dataset). I've got a grid view that takes a sqldatasource and uses that to populate the cells within the gridview. I have not had to create a dataset in any way, and when looking at this example I am not sure of the origin of 'dt'. I am assuming that city.getAllCity() is a custom method that has been made. I am not sure how to approach this either. However, I would not at all mind learning this process and what is actually going on here if you dont mind shedding some light. – jshbrmn May 02 '12 at 08:22
  • Updated example of dataset generation# – Richard May 02 '12 at 08:49
  • Excellent! Thanks for your guidance. Would I be able to insert my sqldatasource into the SqlDataAdapter? I am generating a multitude of queries based on what the user inputs and as a result I cannot utilize just a static sql query/nonquery. – jshbrmn May 02 '12 at 08:52
  • You can take the generated sqlselect command from the datasource as I have shown if that helps – Richard May 02 '12 at 09:02
  • Absolutely. I would have tinkered with this myself had I the time (so much work to do). I really appreciate you sending me this info! – jshbrmn May 02 '12 at 09:08