Button not firing event


I have a page containing a datagrid, which works. On that page I have a button which is supposed to trigger a download of the data from the datagrid in Excel format. However, the button isn’t firing the event.

The C# code-behind I have is:

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 System.Data.SqlClient;
using umbraco;

namespace MyDataExport
public partial class DataExport : System.Web.UI.UserControl
protected void Page_Load(object sender, EventArgs e)
if (!Page.IsPostBack)
GridView1.DataSource = BindData();

    private DataSet BindData()
        string query = "SELECT * FROM budgetData";
        SqlConnection myConnection = new SqlConnection(GlobalSettings.DbDSN);
        SqlDataAdapter ad = new SqlDataAdapter(query, myConnection);
        DataSet ds = new DataSet();
        ad.Fill(ds, "budgetData");
        return ds;

    protected void Button1_Click(object sender, EventArgs e)
        Response.AddHeader("content-disposition", "attachment;filename=FileName.xls");
        Response.Charset = "";
        // Comment out the line below to open the Excel file without saving
        // Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.ContentType = "application/vnd.xls";
        System.IO.StringWriter stringWrite = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);


The .aspx code is:

<%@ Control Language=“C#” AutoEventWireup=“true” CodeBehind=“DataExport.ascx.cs” Inherits=“MyDataExport.DataExport” %>
<input id=“Button1” type=“button” value=“Download” onclick=“Button1_Click” />
<asp:GridView ID=“GridView1” runat=“server”>

When I click the ‘download’ button in a browser to download the excel file, I get a javascript error saying ‘Button1_Click is not defined’.

Can anyone suggest why this isn’t working, and point me in the right direction?


The proble is you are using an input without it being marked as runat server. Or use an <asp:Button>. If the control is not market as runat=“server” then the onlick will be looking for a javascript function

<input id=“Button1” type=“button” value=“Download” onclick=“Button1_Click” />

<input id=“Button1” type=“button” value=“Download” runat=“server” onclick=“Button1_Click” />
<asp:Button runat=“server” id=“Button1” Text=“Download” onclick=“Button1_Click” />