0

当我将网格视图导出到 Excel 时出现此消息错误我的数据库是 SQL Server 并且我的表有 8000 行当我只选择 1 行到 gridview 它导出到 excel 时没有错误但是当我选择所有行时出现这个问题这是我的代码

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    { 
    System.Data.SqlClient.SqlDataReader reader = objCalep.SelectAll(true, false, "[تاريخ الاستشهاد]");
    //    System.Data.SqlClient.SqlDataReader reader = objCCP.CPSelectNo(2700);
    DataTable dataTable = new DataTable();
    dataTable.Load(reader);
    GridView1.DataSource = dataTable;
    GridView1.DataBind();
    }

}

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{

    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        //System.Data.Common.DbDataRecord drv = e.Row.DataItem as System.Data.Common.DbDataRecord;
        DataRowView dataRowView = e.Row.DataItem as DataRowView;
        DataRow drv = dataRowView.Row;

        Object ob = drv["تاريخ الاستشهاد"];
        if (!Convert.IsDBNull(ob))
        {
            DateTime dateTime = (DateTime)ob;
            e.Row.Cells[4].Text = dateTime.ToString("dd/MM/yyyy");
            e.Row.Cells[4].Width = 100;
        }
    }
}
protected void btn_Add_Click(object sender, ImageClickEventArgs e)
{
    PrepareGridViewForExport(GridView1);
    ExportGridView();
}
private void ExportGridView()
{
    string attachment = "attachment; filename=حلب.xls";
    Response.ClearContent();
    Response.AddHeader("content-disposition", attachment);
    Response.ContentType = "application/ms-excel";
    StringWriter sw = new StringWriter();
    HtmlTextWriter htw = new HtmlTextWriter(sw);
    GridView1.RenderControl(htw);
    Response.Write(sw.ToString());
    Response.End();
}
public override void VerifyRenderingInServerForm(Control control)
{
}

private void PrepareGridViewForExport(Control gv)
{
    LinkButton lb = new LinkButton();
    Literal l = new Literal();
    string name = String.Empty;
    for (int i = 0; i < gv.Controls.Count; i++)
    {
        if (gv.Controls[i].GetType() == typeof(LinkButton))
        {
            l.Text = (gv.Controls[i] as LinkButton).Text;
            gv.Controls.Remove(gv.Controls[i]);
            gv.Controls.AddAt(i, l);
        }
        else if (gv.Controls[i].GetType() == typeof(DropDownList))
        {
            l.Text = (gv.Controls[i] as DropDownList).SelectedItem.Text;
            gv.Controls.Remove(gv.Controls[i]);
            gv.Controls.AddAt(i, l);
        }
        else if (gv.Controls[i].GetType() == typeof(CheckBox))
        {
            l.Text = (gv.Controls[i] as CheckBox).Checked ? "True" : "False";
            gv.Controls.Remove(gv.Controls[i]);
            gv.Controls.AddAt(i, l);
        }
        if (gv.Controls[i].HasControls())
        {
            PrepareGridViewForExport(gv.Controls[i]);
        }
    }
}

这是图像错误错误消息

4

1 回答 1

0

好吧,我无法阅读太多该错误消息,但我猜测网格的 ViewState 数据大于您站点上配置的最大请求长度(我相信默认值为 4MB)。

尝试更改web.config 部分<httpRuntime maxRequestLength="158334976" /><system.web>

如果这样可行,您可以将 maxRequestLength 保持在足够高的值,或者为您的网格禁用 ViewState,尽管您必须在回发后再次从数据库中检索数据。

于 2013-04-10T16:22:51.763 回答