SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Guru
    Join Date
    Jun 2004
    Location
    UK
    Posts
    605
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Button not firing event

    Hi,

    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();
    GridView1.DataBind();
    }
    }


    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.Clear();
    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);
    GridView1.RenderControl(htmlWrite);
    Response.Write(stringWrite.ToString());
    Response.End();
    }
    }
    }



    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">
    </asp:GridView>



    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?

    Thanks

  2. #2
    SitePoint Mentor NightStalker-DNS's Avatar
    Join Date
    Jul 2004
    Location
    Cape Town, South Africa
    Posts
    2,878
    Mentioned
    47 Post(s)
    Tagged
    0 Thread(s)
    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

    Replace:
    <input id="Button1" type="button" value="Download" onclick="Button1_Click" />

    with:
    <input id="Button1" type="button" value="Download" runat="server" onclick="Button1_Click" />
    or
    <asp:Button runat="server" id="Button1" Text="Download" onclick="Button1_Click" />


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •