1

我在 c# 的一个类中声明了一个全局变量

 public partial class admin_AnalyseQuery : System.Web.UI.Page
{
    llvsMasterpage m;
    private const string ASCENDING = " ASC";
    private const string DESCENDING = " DESC";
    DataTable dt;

在这里,我创建了一个数据表 dt 作为全局变量,对吗?

现在数据表 dt 填充了屏幕截图中的方法

“BindDatagvAnalyse” - 这是一种将数据绑定到网格视图的方法。

 private void BindDatagvAnalyse()
    {
        Guid SchoolID = Guid.Parse(School.SelectedValue);
        Guid YearID = Guid.Parse(Years.SelectedValue);
        Guid VersionID = Guid.Parse(Versions.SelectedValue);
        Guid LessonID = Guid.Parse(Lessons.SelectedValue);
        Guid TeacherID = Guid.Parse(Teachers.SelectedValue);
        Guid LearningunitID = Guid.Parse(Learningunits.SelectedValue);
        Guid DomainID = Guid.Parse(Domain.SelectedValue);
        pnllbl.Visible = true;
        string Schoolname = School.SelectedItem.Text;
        string Yearname = Years.SelectedItem.Text;
        string Versionname = Versions.SelectedItem.Text;
        string Lessonnmae = Lessons.SelectedItem.Text;
        string Teachername = Teachers.SelectedItem.Text;
        string Learningunitname = Learningunits.SelectedItem.Text;
        string Domainname = Domain.SelectedItem.Text;
        lblinfo.Text = Schoolname + "/" + Yearname + "/" + Lessonnmae + "/" + Versionname + "/" + Teachername + "/" + Domainname + "/" + Learningunitname;
        //  Guid PeriodID = Guid.Parse(Periods.SelectedValue);
        dt = m.bussinesCollection.BussinesDomain.GetAllDetailsForgvanalyse(SchoolID, YearID, LessonID, VersionID, TeacherID, LearningunitID, DomainID);
        int count = dt.Rows.Count;
        lblcount.Text = count.ToString();
        int c = m.bussinesCollection.BussinesDomain.GetAllDetailsForgvanalyseCount(SchoolID, YearID, LessonID, VersionID, TeacherID, LearningunitID, DomainID);
        lblstd.Text = c.ToString();
        gvAnalyse.DataSource = dt;
        gvAnalyse.DataBind();
    }

我已经创建了这种类型的 6 种方法,它们将根据下拉选择创建表,然后调用相应的方法并相应地填充 gvanalysis,这会显示在屏幕上。

现在我放了一个按钮来从表中创建 excel 表。

在“导出 Excel”按钮单击事件中,我必须使用当前显示在屏幕中的表格创建 Excel 工作表。

为此,在按钮单击事件中,我已经这样做了

protected void btnexel_Click(object sender, EventArgs e)
    {
        byte[] Result;
        using (MemoryStream ms = new MemoryStream())
        {
            string school = School.SelectedItem.Text;
            string year = Years.SelectedItem.Text;
            DataTable final = dt;
            // DataTable dt = m.bussinesCollection.BussinesDomain.GetAllDetailsForgvanalyse(SchoolID, YearID, LessonID, VersionID, TeacherID, LearningunitID, DomainID);
            string style = @"<style> .textmode { mso-number-format:\@; } </style>";
            Response.Write(style);
            Result = ms.GetBuffer();
            Session["Excel"] = Result;
            Session["dt"] = final;
            Session["school"] = school;
            Session["year"] = year;
            Session["pdf/excel"] = "excel/loondiest/analyse";
            Response.Redirect("~/admin/DownloadPdf.aspx");
        }
}

但是当我调试它时,它总是显示 table final & dt 都是空的。

我该怎么办?

4

1 回答 1

0

您必须坚持您的DataTablein persistance structure,因为当您发布数据时,您的数据会丢失。

持久性结构

ViewState : on page with post

Session

Cache

Application

样本

public DataTable Table
{
  get
  {   
      if(ViewState["KeyTable"] != null)
      {
         return ViewState["KeyTable"] as DataTable;
      }
  }
  set
  {
     ViewState["KeyTable"] = value;
  }
} 

用例:

Table = your value;

.....

表 =

于 2012-09-13T10:02:13.240 回答