2012-12-01 165 views
-1

按下按鈕後,我需要將頁面2上的控件的數據傳輸到頁面1上的GridView控件。 (不使用數據庫)ASP.NET - 兩頁之間的數據傳輸

我嘗試使用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(); 
    } 
} 

我有什麼錯?

+0

哪裏是存儲DataTable實例進入會話的代碼? – sll

回答

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

關於如何解決它的任何提示? – cvenko

+0

完成它,仍然拋出相同的異常,但在下一行,我分配值 – cvenko

+0

沒有名爲「Vrsta avtomobila」的行,因此會在dr [「Vrsta avtomobila」]上拋出異常。嘗試調試流程並添加「dt」對象來觀察窗口,以確切地查看它包含的內容。 – Sunny

2

你得到錯誤,becouse會話[「tabela」]之前是空的。所以insted的這段代碼:

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

所以,你將永遠不會空的DataTable。

+0

完成它,仍然開始相同的錯誤 – cvenko

+0

@ cvenko我已經試過你的代碼和例外是「博士[」Vrsta avtomobila「]]」不存在。 DataRow dr = dt.NewRow();工作正常 –