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?