有没有使用它的替代方法,因为使用 a<form runat="server">
会导致 500 错误。
public override void VerifyRenderingInServerForm(Control control)
{
/* Verifies that the control is rendered */
}
已编辑
在@HansDerks 的帮助下,我最终使用了以下内容(提供的解决方案的爵士版本。):
protected void Export_Click(object sender, System.EventArgs e)
{
StringWriter writer = new StringWriter();
HtmlTextWriter htmlWriter = new HtmlTextWriter(writer);
GridView gridView = new GridView();
gridView.DataSource = MySqlDataSource;
gridView.AutoGenerateColumns = true;
gridView.DataBind();
gridView.HeaderRow.Style.Add("background-color", "#003c74");
gridView.HeaderRow.Style.Add("color", "#ffffff");
for (int i = 0; i < gridView.Rows.Count; i++)
{
GridViewRow row = gridView.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.BackColor = System.Drawing.Color.AliceBlue;
}
}
gridView.RenderControl(htmlWriter);
htmlWriter.Close();
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=filename.xls");
Response.Charset = "";
Response.Write(writer.ToString());
Response.End();
}
我希望你们会发现它有用。谢谢大家!