Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ASP.NET-两页之间的数据传输_C#_Asp.net - Fatal编程技术网

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

C# ASP.NET-两页之间的数据传输,c#,asp.net,C#,Asp.net,按下按钮后,我需要将数据从第2页的控件传输到第1页的GridView控件。(不使用数据库) 我尝试使用DataTable存储数据并将它们排列成列/行 但当我点击按钮时,我得到一个异常,在第58行中说:“对象引用未设置为对象的实例。”DataRow dr=dt.NewRow() 第2页c#代码: public partial class WebForm1 : System.Web.UI.Page { //Lastnosti public string IDizposoje

按下按钮后,我需要将数据从第2页的控件传输到第1页的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;
    }
}
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();
    }
}
第1页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;
    }
}
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();
    }
}
我哪里出错了

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();
    }
受保护的无效页面加载(对象发送方,事件参数e)
{
WebForm1 druga=新的WebForm1();
DataTable tabela=UstvariTabelo();
NapolniTabelo(塔贝拉);
this.GridView1.Visible=true;

Session[“tabela”]=tabela;//出现错误,因为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;
   }
}

因此,您将永远不会得到空的DataTable。

将DataTable实例存储到会话中的代码在哪里?完成后,仍会引发相同的异常,但在下一行,我分配了值。没有名为“Vrsta avtomobila”的行,因此将在dr[“Vrsta avtomobila”]上引发异常。请尝试调试流并添加“dt”对象来监视窗口,以查看它到底包含什么。@cvenko我已尝试了您的代码,但“dr[“Vrsta avtomobila”]”上出现异常,该异常不存在。DataRow dr=dt.NewRow();工作正常