-1

我需要在按下按钮时将数据从 page2 上的控件传输到 page1 上的 GridView 控件。(不使用数据库)

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

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

第2页c#代码:

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;
    }
}

第1页c#代码:

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();
    }
}

我哪里做错了?

4

2 回答 2

2

你得到错误,因为 Session["tabela"] 是空的。所以插入了这段代码:

(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;
   }
}

所以你永远不会得到空数据表。

于 2012-12-01T18:42:10.683 回答
0
DataRow dr = dt.NewRow(); 

它引发异常,因为数据表对象(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();
    }
于 2012-12-01T18:33:28.807 回答