Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7866365
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T00:22:33+00:00 2026-06-03T00:22:33+00:00

Ok so here is the situation. I have Gridview content being exported to an

  • 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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T00:22:35+00:00Added an answer on June 3, 2026 at 12:22 am

    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");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is my situation: I have one table that contains a list of drugs
Here's the situation: I have a two images that are over 1024 in width
Here is the situation: I have a link of a page that gives only
Here's the situation: I have a custom TextBox control that contains multiple other TextBox
So here is the situation - I have a store app that I want
Here's my situation: I have a file, homepage.php, that I want to allow my
Here's the situation: We have some generic graphics code that we use for one
Here's the situation: I have an abstract class with a constructor that takes a
Here is my situation: I have an application that use a configuration file. The
Here is the situation: I have a webpage that I have scraped as a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.