Export GridView to Excel

1

I try to export my GridView to a file in Excel but it marks me error when clicking on the button to export, although it says that it must be in Form with runat="server" , the GridView is in form

This error marks me:

  

Control 'ctl00_Grv_main' of type 'GridView' must be placed inside a tag with runat = server.

These are the Using :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Data;
using System.Data.OleDb;

This is my BackEnd :

protected override void Render(HtmlTextWriter writer)
        {
            /// this is needed to render your new control.
            base.Render(writer);
        } 

This is the function:

private void ExportGridToExcel()
        {
            Response.Clear();
            Response.Buffer = true;
            Response.ClearContent();
            Response.ClearHeaders();
            Response.Charset = "";
            string FileName = "RepoToolMaint" + DateTime.Now + ".xls";
            StringWriter strwritter = new StringWriter();
            HtmlTextWriter htmltextwrtter = new HtmlTextWriter(strwritter);
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.ContentType = "application/vnd.ms-excel";
            Response.AddHeader("Content-Disposition", "attachment:filename=" + FileName);
            Grv_main.GridLines = GridLines.Both;
            Grv_main.HeaderStyle.Font.Bold = true;
            Grv_main.RenderControl(htmltextwrtter);
            Response.Write(strwritter.ToString());
            Response.End();
        }

And this the button where I call the function

protected void Download_repo(object sender, EventArgs e)
        {
            ExportGridToExcel();
        }

This is the HTML :

<form id="form1" runat="server">
    <div class="container">
        <div class="modal fade" id="History-Maint" role="dialog">
            <div class="modal-dialog modal-lg">
                <div class="modal-content">
                    <div class="modal-header" style="padding: 35px 50px;">
                        <button type="button" class="close" data-dismiss="modal">&times;</button>
                        <h3><span class="glyphicon glyphicon-list-alt"></span>&nbsp Maintenance Report</h3>
                    </div>
                    <div class="modal-body">
                        <asp:GridView ID="Grv_main"
                            CssClass="table table-striped table-bordered table-hover"
                            runat="server"
                            AutoGenerateColumns="False"
                            Width="940px"
                            HorizontalAlign="Center"
                            DataKeyNames="id"
                            AllowPaging="True" OnRowEditing="GridView1_RowEditing">
                            <Columns>
                                <asp:TemplateField HeaderText="id" Visible="false">
                                    <ItemTemplate>
                                        <asp:Label ID="Lbl_id" runat="server" Text='<%# Bind("id") %>'></asp:Label>
                                    </ItemTemplate>
                                    <ItemStyle HorizontalAlign="Center" />
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="status">
                                    <ItemTemplate>
                                        <asp:Label ID="Lbl_stats" runat="server" Text='<%# Bind("STATUS") %>'></asp:Label>
                                    </ItemTemplate>
                                    <HeaderStyle HorizontalAlign="Center" />
                                    <ItemStyle HorizontalAlign="Center" />
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="tool">
                                    <ItemTemplate>
                                        <asp:Label ID="Lbl_tool" runat="server" Text='<%# Bind("TOOL") %>'></asp:Label>
                                    </ItemTemplate>
                                    <HeaderStyle HorizontalAlign="Center" />
                                    <ItemStyle HorizontalAlign="Center" />
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Area Change">
                                    <ItemTemplate>
                                        <asp:Label ID="Lbl_areach" runat="server" Text='<%# Bind("AREA") %>'></asp:Label>
                                    </ItemTemplate>
                                    <HeaderStyle HorizontalAlign="Center" />
                                    <ItemStyle HorizontalAlign="Center" />
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="responsable">
                                    <ItemTemplate>
                                        <asp:Label ID="Lbl_resp" runat="server" Text='<%# Bind("RESPONSABLE") %>'></asp:Label>
                                    </ItemTemplate>
                                    <HeaderStyle HorizontalAlign="Center" />
                                    <ItemStyle HorizontalAlign="Center" />
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="nota">
                                    <ItemTemplate>
                                        <asp:Label ID="Lbl_note" runat="server" Text='<%# Bind("NOTA") %>'></asp:Label>
                                    </ItemTemplate>
                                    <HeaderStyle HorizontalAlign="Center" />
                                    <ItemStyle HorizontalAlign="Center" />
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="fecha de modificacion">
                                    <ItemTemplate>
                                        <asp:Label ID="Lbl_mod" runat="server" Text='<%# Bind("MODIFY") %>'></asp:Label>
                                    </ItemTemplate>
                                    <HeaderStyle HorizontalAlign="Center" />
                                    <ItemStyle HorizontalAlign="Center" />
                                </asp:TemplateField>
                            </Columns>
                        </asp:GridView>
                    </div>
                    <div class="modal-footer">
                        <asp:Button ID="Button4" runat="server" CssClass="btn btn-success" Text='Guardar' OnClick="Download_repo" />
                    </div>
                </div>
            </div>
        </div>
    </div>
</form>
    
asked by Cesar Gutierrez Davalos 05.06.2017 в 22:46
source

1 answer

1

Well, you do not specify if that form is part of the html of a masterpage or a webform "common". If you are using MasterPage , this should have a <form></form> inside .... and the content of the forms that you will show through MasterPage must be within that form which is done through contentPlaceHolder therefore, this must also be within your master page ... like this:

<html>
  ...
  <body>
    <form id="form1" runat="server">
      <h1>Soy un MasterPage!</h1>
      <asp:contentplaceholder id="ContentPlaceHolder1" runat="server" />
    </form>
  </body>
  ...
</html>

If your masterpage already contains a <form> it is not necessary to add it to the WebForm content that you try to visualize through the contentPLaceHolder of your masterpage.

Initially verify that you are not using 2 form tags (one in your masterpage and one in your webform )

    
answered by 05.06.2017 в 23:23