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 6090593
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:13:54+00:00 2026-05-23T12:13:54+00:00

I want to covnvert GridView data to Excel sheet. I have written the code

  • 0

I want to covnvert GridView data to Excel sheet.

I have written the code below, but it gives error:

protected void Button1_Click(object sender, EventArgs e) 
{       
    Response.Clear();
    Response.AddHeader("content-disposition", "attachment;filename=Avukat.xls");
    Response.Charset = "";

    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("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />");
    Response.Write(stringWrite.ToString());
    Response.End();
}

Error:

Control ‘ctl00_ContentPlaceHolder1_GridView1’ of type ‘GridView’ must be placed inside a form tag with runat=server.

  • 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-05-23T12:13:54+00:00Added an answer on May 23, 2026 at 12:13 pm

    I think your gridview contains a linkbutton/Imagebutton or another type of control, and that’s why you are getting an Exception when you are trying to export the GridView to Excel.

    Before using the control you need to add the following lines in your Page code behind, or the BasePage code behind.

    public override void VerifyRenderingInServerForm(Control control)
    {
    }
    

    You can use this code, as this code is tested and worked perfectly:

    System.IO.StringWriter sw = new System.IO.StringWriter();
    HtmlTextWriter htw = new HtmlTextWriter(sw);
    Response.AddHeader("content-disposition", "attachment; filename=Avukat.xls");
    Response.ClearContent();
    
    Response.AddHeader("content-disposition", attachment);
    
    GridView1.RenderControl(htw);
    Response.Write(sw.ToString());
    Response.Flush();
    Response.End();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a gridView binded to a data source, and I have a ButtonField
I want to populate a gridview with table data from SQL. I tried just
I want to write parametrized query for select statement. but it gives exception that
Possible Duplicate: GridView current page I have a GridView ,I enabled Paging. I want
I want to convert my GridView to a DataTable. Note: The GridView doesn't have
I want to display a total in the GridView footer - nothing unusual there.
I have a GridView with a ButtonField of type link <asp:ButtonField ButtonType=Link CommandName=more HeaderText=Name
I want to show icon that associated with file type. Something like this But
I have a bound field in my Gridview which is getting its value from
I've created a gridview dynamically, now i want to fire an event when the

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.