Reputation: 39
I have a asp button which does not fire event after clicking. The button is inside a update panel.
<asp:Button ID="btnExportExcel" runat="server" Text="Export to Excel" CssClass="buttonsmall" BorderStyle="None" OnClick="btnExportExcel_Click" />
I also added the following code on page load
ScriptManager scriptManager = ScriptManager.GetCurrent(this.Page);
scriptManager.RegisterPostBackControl(this.btnExportExcel);
C# code
protected void btnExportExcel_Click(object sender, EventArgs e)
{
try
{
if (dt.Rows.Count > 0)
{
string filename = "List.xls";
System.IO.StringWriter sw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(sw);
GridView dgGrid = new GridView();
dgGrid.RowDataBound += gdv_RowDataBound;
dgGrid.DataSource = dt;
dgGrid.DataBind();
dgGrid.HeaderRow.Style.Add("background-color", "#FFFFFF");
dgGrid.HeaderRow.Cells[0].Visible = false;
//Apply style to Individual Cells
dgGrid.HeaderRow.Cells[0].Style.Add("background-color", "#A59CFF");
dgGrid.HeaderRow.Cells[1].Style.Add("background-color", "#A59CFF");
dgGrid.HeaderRow.Cells[2].Style.Add("background-color", "#A59CFF");
dgGrid.HeaderRow.Cells[3].Style.Add("background-color", "#A59CFF");
dgGrid.HeaderRow.Cells[4].Style.Add("background-color", "#A59CFF");
dgGrid.HeaderRow.Cells[5].Style.Add("background-color", "#A59CFF");
dgGrid.HeaderRow.Cells[6].Style.Add("background-color", "#A59CFF");
dgGrid.HeaderRow.Cells[7].Style.Add("background-color", "#A59CFF");
dgGrid.HeaderRow.Cells[8].Style.Add("background-color", "#A59CFF");
dgGrid.HeaderRow.Cells[9].Style.Add("background-color", "#A59CFF");
dgGrid.HeaderRow.Cells[10].Style.Add("background-color", "#A59CFF");
dgGrid.HeaderRow.Cells[11].Style.Add("background-color", "#A59CFF");
dgGrid.HeaderRow.Cells[12].Style.Add("background-color", "#A59CFF");
for (int i = 0; i < dgGrid.Rows.Count; i++)
{
GridViewRow row = dgGrid.Rows[i];
//Change Color back to white
row.BackColor = System.Drawing.Color.White;
//Apply text style to each Row
row.Attributes.Add("class", "textmode");
//Apply style to Individual Cells of Alternating Row
if (i % 2 != 0)
{
row.Cells[0].Style.Add("background-color", "#A59CFF");
row.Cells[1].Style.Add("background-color", "#A59CFF");
row.Cells[2].Style.Add("background-color", "#A59CFF");
row.Cells[3].Style.Add("background-color", "#A59CFF");
row.Cells[4].Style.Add("background-color", "#A59CFF");
row.Cells[5].Style.Add("background-color", "#A59CFF");
row.Cells[6].Style.Add("background-color", "#A59CFF");
row.Cells[7].Style.Add("background-color", "#A59CFF");
row.Cells[8].Style.Add("background-color", "#A59CFF");
row.Cells[9].Style.Add("background-color", "#A59CFF");
row.Cells[10].Style.Add("background-color", "#A59CFF");
row.Cells[11].Style.Add("background-color", "#A59CFF");
row.Cells[12].Style.Add("background-color", "#A59CFF");
}
}
dgGrid.RenderControl(hw);
Response.ContentType = "application/vnd.ms-excel";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename + "");
this.EnableViewState = false;
//style to format numbers to string
string style = @"<style> .textmode { mso-number-format:\@; } </style>";
Response.Write(style);
Response.Output.Write(sw.ToString());
Response.End();
Response.Flush();
dgGrid.Dispose();
}
}
Am i going wrong some where
Upvotes: 2
Views: 653
Reputation: 39
I have fixed the issue
The root cause of the problem was that I had a jQuery validations on the page even though i put causes validation=false on button since jQuery being on the clientside it did not work
The issue can be fixed by adding $("form").validate().cancelSubmit = true; n the jQuery click of the button
Upvotes: 1
Reputation: 14860
If the button triggers an async postback you are most likely receiving an error message behind the scenes since you are modifying the response. Add the following to the Triggers
collection in the update panel so that the button triggers a full postback...
<Triggers>
<asp:PostBackTrigger ControlID="btnExportExcel" />
</Triggers>
Then, remove the following line of code which is not needed...
ScriptManager scriptManager = ScriptManager.GetCurrent(this.Page);
scriptManager.RegisterPostBackControl(this.btnExportExcel);
and make sure you have set the AutoEventWireup
property of the page directive to true
...
<%@ Page Title="" Language="C#" AutoEventWireup="true" %>
Upvotes: 0