asp:按钮事件未触发

时间:2014-03-18 03:38:29

标签: asp.net

我有一个asp按钮,点击后不会触发事件。该按钮位于更新面板内。

<asp:Button ID="btnExportExcel" runat="server" Text="Export to Excel" CssClass="buttonsmall"  BorderStyle="None" OnClick="btnExportExcel_Click" />

我还在页面加载

上添加了以下代码
   ScriptManager scriptManager = ScriptManager.GetCurrent(this.Page);
   scriptManager.RegisterPostBackControl(this.btnExportExcel);

C#代码

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

我在哪里错了

2 个答案:

答案 0 :(得分:1)

我已修复此问题

问题的根本原因是我在页面上有一个jQuery验证,即使我在按钮上放置cause validation = false,因为jQuery在客户端上它不起作用

可以通过添加$(&#34; form&#34;)来修复此问题.validate()。cancelsubmit = true; 在jQuery上单击按钮

答案 1 :(得分:0)

如果按钮触发异步回发,则很可能在您修改响应后会在后台收到错误消息。将以下内容添加到更新面板中的Triggers集合中,以便按钮触发完整的回发...

<Triggers>
      <asp:PostBackTrigger ControlID="btnExportExcel" />
 </Triggers>

然后,删除不需要的以下代码行......

ScriptManager scriptManager = ScriptManager.GetCurrent(this.Page);
scriptManager.RegisterPostBackControl(this.btnExportExcel);

并确保您已将页面指令的AutoEventWireup属性设置为true ...

<%@ Page Title="" Language="C#" AutoEventWireup="true" %>