我需要在按下按钮时将数据从 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();
}
}
我哪里做错了?