0

我正在使用下面的代码来导出 gridview 数据。这在 ascx 页面中工作正常。但现在我创建 ascx 页面并在此 ascx 页面中使用相同的代码。但这对我不起作用。请帮助我做到这一点。

try
            {
                DataSet ds = new DataSet();
                ds = (DataSet)Session["Datasource"];
                GridView grid = new GridView();
                Response.ClearContent();
                Response.Buffer = true;
                Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "Record.xls"));
                Response.ContentType = "application/ms-excel";
                grid.DataSource = ds;
                grid.DataBind();
                grid.AllowPaging = false;
                StringWriter sw = new StringWriter();
                HtmlTextWriter htw = new HtmlTextWriter(sw);
                //Change the Header Row back to white color
                grid.HeaderRow.Style.Add("background-color", "#FFFFFF");
                //Applying stlye to gridview header cells
                for (int i = 0; i < grid.HeaderRow.Cells.Count; i++)
                {
                    grid.HeaderRow.Cells[i].Style.Add("background-color", "#507CD1");
                    grid.FooterRow.Cells[i].Style.Add("background-color", "#507CD1");
                }
                int j = 1;
                //This loop is used to apply stlye to cells based on particular row
                foreach (GridViewRow gvrow in grid.Rows)
                {
                    gvrow.BackColor = System.Drawing.Color.White;
                    if (j <= grid.Rows.Count)
                    {
                        if (j % 2 != 0)
                        {
                            for (int k = 0; k < gvrow.Cells.Count; k++)
                            {
                                gvrow.Cells[k].Style.Add("background-color", "#EFF3FB");
                            }
                        }
                    }
                    j++;
                }
                grid.RenderControl(htw);
                Response.Write(sw.ToString());
                Response.End();

            }
            catch (Exception e1)
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "onload", "<script language='javascript'>alert('" + e1.Message + "');</script>", false);
            }
4

1 回答 1

0

VerifyRenderingInServerFormis类的方法Page,所以你需要将它添加到包含 UserControl 的页面中

public override void VerifyRenderingInServerForm(Control control)
{
/* Confirms that an HtmlForm control is rendered for the specified ASP.NET
server control at run time. */
}
于 2013-10-08T06:09:09.770 回答