This article explains how you can export your GridView control to Excel files. If you are working with web user control. Suppose if you are having your gridview in your User Control and you are trying to export the gridview data to some other format some thing like this
protected void _btnReports_Click(object sender, EventArgs e)
{
Response.Clear();
Response.Write(@”<!DOCTYPE HTML PUBLIC “”-//W3C//DTD HTML 4.0 Transitional//EN”">”);
Response.AddHeader(“content-disposition”, “inline;filename=search_topicarea.doc”);
Response.Charset = “”;
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = “application/vnd.doc”;
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
//System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
//Render(htmlWrite);
_gvViewresults.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();
}
And adding a little piece of code like override the Page.VerifyRenderingInServerForm method as shown below
public override void VerifyRenderingInServerForm(Control control)
{
// Confirms that an HtmlForm control is rendered for the
//specified ASP.NET server control at run time.
}
But this works in aspx page but you cannot override the Page.VerifyRenderingInServerForm method in the web user control.
In order to solve the above problem:
1. Create one aspx page
2. Then drag the web user control in to this aspx page but your grid view and the button click events will be in the web user control only.
3. Then in the aspx page in the Page directives tag just add EnableEventValidation=”false”.
4. In the code behind file i.e. aspx.cs file now override the Page.VerifyRenderingInServerForm method.
5. This works.
Sample code is given below:
<%@ Page Language=”C#” MasterPageFile=”~/Masters/abc.master” AutoEventWireup=”true”
CodeFile=”create.aspx.cs” Inherits=”abc_ create “ EnableEventValidation=”false”
Title=”create” %>
<%@ Register Src=”../UserControls/View.ascx” TagName=”View” TagPrefix=”uc1″ %>
<%@ Register Assembly=”AjaxControlToolkit” Namespace=”AjaxControlToolkit” TagPrefix=”EA” %>
:Content ID=”Content1″ ContentPlaceHolderID=”MainContent” runat=”Server”>
h2 class=”blue”>
View Pending Enquiries
h2>
p>
uc1: View id=”_View” runat=”server”>
uc1: View ></p>
asp:Content>
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using EA.SEDB.BO.ScienceEnquiriesDBTableAdapters;
using EA.SEDB.BO;
using EA.SEDB.BS;
public partial class abc_view : System.Web.UI.Page
{
#region Declare Variables Start
Abc.UsersDataTable UsersDT = null;
string userName = string.Empty;
int loggedInUserId;
#endregion Declare Variables End
#region PageLoad Start
protected void Page_Load(object sender, EventArgs e)
{
userName = HttpContext.Current.User.Identity.Name;
if (!Page.IsPostBack)
{
if (!string.IsNullOrEmpty(userName))
{
UsersDT = Administration.GetUserDetailsByUsername(userName);
if (UsersDT.Rows.Count > 0)
{
loggedInUserId = UsersDT[0].UserId;
}
}
if (HttpContext.Current.User.Identity.IsAuthenticated)
{
if (Roles.IsUserInRole(ConfigurationManager.AppSettings["Managers"]))
{
_ViewEnquiries.ViewEnquiries_ForManager(loggedInUserId, ConfigurationManager.AppSettings["AssignTo"].ToString());
}
else if (Roles.IsUserInRole(ConfigurationManager.AppSettings["TeamMembers"]))
{
_ViewEnquiries.ViewEnquiries_ForTeamMember(loggedInUserId, ConfigurationManager.AppSettings["Assign"].ToString());
}
else
{
Response.Redirect(“~/UnauthorisedPage.aspx”);
}
}
else
{
Response.Redirect(“~/UnauthorisedPage.aspx”);
}
}
}
public override void VerifyRenderingInServerForm(Control control)
{
// Confirms that an HtmlForm control is rendered for the
//specified ASP.NET server control at run time.
}
#endregion PageLoad End
}
Happy Coding…….