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">×</button>
<h3><span class="glyphicon glyphicon-list-alt"></span>  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>