ASP.NET - 两页之间的数据传输

时间:2012-12-01 18:28:59

标签: c# asp.net

我需要在按下按钮时将第2页上的控件的数据传输到第1页的GridView控件。 (不使用数据库)

我尝试使用DataTable存储数据并将它们排列成列/行。

但是当我点击按钮时,我得到一个异常说:“对象引用没有设置为对象的实例。”在第58行。 - > DataRow dr = dt.NewRow();

Page2 c#code:

public partial class WebForm1 : System.Web.UI.Page
{
    //Lastnosti
    public string IDizposoje
    {
        get { return TextBox3.Text; }
    }

    public string Ime
    {
        get { return TextBox1.Text; }
    }

    public string Priimek
    {
        get { return TextBox2.Text; }
    }

    public string DatumIzposoje
    {
        get { return Calendar1.SelectedDate.ToString(); }
    }

    public string DatumVrnitve
    {
        get { return Calendar2.SelectedDate.ToString(); }
    }

    public string VrstaAvtomobila
    {
        get { return ListBox1.SelectedItem.Text; }
    }

    //Koda, ki se izvrši ob zagonu
    protected void Page_Load(object sender, EventArgs e, DataTable dt)
    {         
    }

    //Ob kliku na gumb "Prekliči" zapremo stran
    protected void Button2_Click(object sender, EventArgs e)
    {
        //Response.Redirect("~/Default.aspx");
        this.ClientScript.RegisterClientScriptBlock(this.GetType(), "Close", "window.close()", true);
    }

    //Napolnimo tabelo s podatki
    public void NapolniTabelo(DataTable dt)
    {
        DataRow dr = dt.NewRow();

        dr["ID"] = TextBox3.Text;
        dr["Ime"] = TextBox1.Text;
        dr["Priimek"] = TextBox2.Text;
        dr["Datum izposoje"] = Calendar1.SelectedDate.ToString();
        dr["Datum vrnitve"] = Calendar2.SelectedDate.ToString();
        dr["Vrsta avtomobila"] = ListBox1.SelectedValue.ToString();

        dt.Rows.Add(dr);
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        NapolniTabelo((DataTable)Session["tabela"]);
        /*Session["ID"] = TextBox3.Text;
        Session["Ime"] = TextBox1.Text;
        Session["Priimek"] = TextBox2.Text;
        Session["Datum izposoje"] = Calendar1.SelectedDate.ToString();
        Session["Datum vrnitve"] = Calendar2.SelectedDate.ToString();
        Session["Vrsta avtomobila"] = ListBox1.SelectedValue.ToString();*/
        Response.Redirect("Default.aspx");
    }

    //Ponastavimo gradnike
    protected void Button3_Click(object sender, EventArgs e)
    {
        TextBox1.Text = "";
        TextBox2.Text = "";
        TextBox3.Text = "";
        Calendar1.SelectedDate = DateTime.Now;
        Calendar2.SelectedDate = DateTime.Now;
        ListBox1.SelectedIndex = 0;
    }
}

Page1 c#code:

public partial class _Default : System.Web.UI.Page
{
    private DataTable UstvariTabelo()
    {
        DataTable dt = new DataTable();

        dt.Columns.Add(new DataColumn("ID", typeof(string)));
        dt.Columns.Add(new DataColumn("Ime", typeof(string)));
        dt.Columns.Add(new DataColumn("Priimek", typeof(string)));
        dt.Columns.Add(new DataColumn("Datum izposoje", typeof(string)));
        dt.Columns.Add(new DataColumn("Datum vrnitve", typeof(string)));
        dt.Columns.Add(new DataColumn("Vrsta vozila", typeof(string)));

        return dt;
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        WebForm1 druga = new WebForm1();
        DataTable tabela = UstvariTabelo();

        druga.NapolniTabelo(tabela);

        this.GridView1.Visible = true;
        this.GridView1.DataSource = ((DataTable)Session["tabela"]);
        this.GridView1.DataBind();
    }
}

我哪里出错了?

2 个答案:

答案 0 :(得分:2)

您收到错误,因为Session [“table”]为空。所以代替这段代码:

(DataTable)Session["tabela"]

始终使用安全财产:

public DataTable tabela
{

   get
   {
      if(HttpContext.Current.Session["tabela"] == null)
      {
           HttpContext.Current.Session["tabela"] = new DataTable ("tableName");
      }
      return HttpContext.Current.Session["tabela"] as DataTable;
   }
   set
   {
      HttpContext.Current.Session["tabela"] = value;
   }
}

所以你永远不会得到null DataTable。

答案 1 :(得分:0)

DataRow dr = dt.NewRow(); 

它正在引发异常,因为datatable对象(dt)不在内存中并且您正在尝试访问它。为了将其保留在两个页面中,您可以使用会话变量。例如:

第1页:

DataTable dt =  UstvariTabelo(); //I think this method is returning data table in page 1
//load data into dt
Session["test"]  = dt; //save it into a session variable

在第2页中,您可以检索已保存的会话值

if(Session["test"]!=null)
{
  DataTable dt = (DataTable) Session["test"];
}

同样在第1页的代码中,在第2页检索之前,您没有分配任何会话变量。

protected void Page_Load(object sender, EventArgs e)
    {
        WebForm1 druga = new WebForm1();
        DataTable tabela = UstvariTabelo();

        druga.NapolniTabelo(tabela);

        this.GridView1.Visible = true;
        Session["tabela"] = tabela;//<--------assign it to session
        this.GridView1.DataSource = tabela;
        this.GridView1.DataBind();
    }