2016-03-01 54 views
0

發送的DataGridView到一個新的頁面我有其中一個用戶提交使用一個jQuery的DatePicker的日期範圍Web表單。然後 的DatePicker的連接到數據庫和檢索信息,並顯示一個DataGridView從用戶提交

protected void DataGrid1(string FirstDate, string SecondDate) 
    { 
     DateTime fFirstDate; 
     DateTime sSecondDate; 
     DataTable dt = new Datatable(); 
     DataTable dt2 = new DataTable(); 

     //Check Valid Date Format 

     if (DateTime.TryParse(FirstDate, out fFirstDate) && DateTime.TryParse(SecondDate, out sSecondDate)) 
     { 
      SqlConnection con = new SqlConnection(GetConnectionString()); 
      try 
      { 
       con.Open(); 
       string sqlStatement = "SELECT * @DateFrom and @DateTo" 
       SqlCommand cmd = new SqlCommand(SqlStatement, con); 
       cmd.Parameters.AddWithValue("@DateFrom", fFirstDate) 
       cmd.Parameters.AddWithValue("@DateTo", sSecondDate) 
       sqlDataAdapter sql_adapter = new SqlDataAdapter(cmd); 
       sql_adapter.Fill(dt); 
       if (dt.Rows.Count > 0) 
       { 
        GridView1.DataSource = dt; 
        GridView1.DataBind(); 
       } 
       else 
       {} 
      catch (System.Data.SqlClient.SqlException ex) 
      { 
       string msg = "Error" 
       msg += ex.Message; 
       throw new Exception(msg); 
      } 
      finally 
      { 
       con.Close(); 
      } 

      //Repeat Try for DataTable2 creating new data source 


     } 

    } 

上面我有我的代碼在數據庫中檢索,從提交日期範圍內的用戶的信息。以下是按鈕點擊代碼。

protected void Button1_Click(object sender, EventArgs e) 
    { 
     BindDataGrid1(TextFirstDate.Text, TextSecondDate.Text); 
    } 

當用戶輸入日期時,DataGridView顯示在同一頁面上。提交後有沒有辦法在新的webform上顯示datagridview?

我嘗試添加重定向到按鈕,一個新頁面,並建立HTML代碼的新頁面上的DataGridView,但我似乎無法得到因爲它的一個新的形式,它是正確的。

+0

如果它聲明爲公共靜態數據表,你可以該數據表存儲在會話,並使用它轉換回一個DataTable中的'as'關鍵字,如果你想在另一頁上要顯示在DataGridView ..你將不得不到模板複製並粘貼到aspx頁面。 。或者將datagridview放在另一個表單上,設置auto generate columns = true ..並將數據表綁定到datagridview以及分配GridView.DataSource ..第二個表單需要一個'數據表variable'宣佈在類級別這其實並不難 – MethodMan

回答

0

我使用DataGrid1(string FirstDate, string SecondDate),你需要返回DataTable的結果,並且這種結果存儲到session做了一些修改到您的code.By。例如

protected DataTable DataGrid1(string FirstDate, string SecondDate) 
    { 
     DateTime fFirstDate; 
     DateTime sSecondDate; 
     DataTable dt = new Datatable(); 
     DataTable dt2 = new DataTable(); 

     //Check Valid Date Format 

     if (DateTime.TryParse(FirstDate, out fFirstDate) && DateTime.TryParse(SecondDate, out sSecondDate)) 
     { 
      SqlConnection con = new SqlConnection(GetConnectionString()); 
      try 
      { 
       con.Open(); 
       string sqlStatement = "SELECT * @DateFrom and @DateTo" 
       SqlCommand cmd = new SqlCommand(SqlStatement, con); 
       cmd.Parameters.AddWithValue("@DateFrom", fFirstDate) 
       cmd.Parameters.AddWithValue("@DateTo", sSecondDate) 
       sqlDataAdapter sql_adapter = new SqlDataAdapter(cmd); 
       sql_adapter.Fill(dt); 
       return dt; 
      catch (System.Data.SqlClient.SqlException ex) 
      { 
       string msg = "Error" 
       msg += ex.Message; 
       throw new Exception(msg); 
      } 
      finally 
      { 
       con.Close(); 
      } 
     } 
    } 

而在按鈕點擊事件調用此函數並將其值存儲到Session例如,

 protected void Button1_Click(object sender, EventArgs e) 
    {  Session.Add("MyGridData",BindDataGrid1(TextFirstDate.Text,TextSecondDate.Text)); 
      Response.Redirect("Webform2.aspx"); 
    } 

而且現在把你的網格到您的Webform2.aspx和寫下面的代碼後面的代碼回傳,你可以在`dt`拿到後

protected void Page_Load(object sender, EventArgs e) 
    { 
     DataTable dt= (DataTable)Session["MyGridData"]; 
     if (dt.Rows.Count > 0) 
     { 
      GridView1.DataSource = dt; 
      GridView1.DataBind(); 
     } 
    } 
+0

我需要回到2個數據網格,它是正確的啓動2個SQL語句和命令? – walangala

+0

是的。您可以創建一個StoredProcedure,它返回兩個結果表並將其存儲到DataSet中,並從您的函數中重新生成此DataSet,並將此DataSet添加到Session中。在第二頁上從會話中獲取數據例如 DataSet ds =(DataSet)Session [「MyGridData」]; (ds.Table [0] .Rows.Count> 0) { GridView1.DataSource = ds.Table [0]; GridView1.DataBind(); } if(ds.Table [1] .Rows.Count> 0) { GridView2.DataSource = ds.Table [1]; GridView2.DataBind(); } –

+0

如果您使用Storedprocedure,則會更好。如果你不想使用存儲過程,那麼在DataTable中啓動2個sql語句和命令並存儲返回表,並將所有這些tbales添加到DataSet中。並休息。 –

0

我只想創建與它的DataGrid的另一頁,並傳遞日期參數,查詢字符串,以它的按鈕點擊。

像這樣:

頁巴頓和日期選擇器:

protected void Button1_Click(object sender, EventArgs e) 
{ 
    Response.Redirect(string.Format("MyDataGridPage.aspx?firstDate={0}&secondDate={1}", TextFirstDate.Text, TextSecondDate.Text)); 
} 

的DataGrid頁:

protected void Page_Load(object sender, EventArgs e) 
    { 
     if (!Page.IsPostBack) 
     { 
      var firstDate = Request.QueryString["firstDate"] ?? DateTime.Now; 
      var secondDate = Request.QueryString["secondDate"] ?? DateTime.Now.AddDays(1); 

      BindDataGrid1(firstDate, secondDate); 
     } 
    } 
0

您應該使用跨頁後。 在按鈕把回髮網址這樣

在投稿頁面,您可以在頁面加載事件 保護無效的Page_Load(對象發件人,EventArgs的){ 文本框 得到txtstartdate日期輸入的值,這樣的; TextBox txtEnddate;

 //getting controls from previous page 
    txtstartdate= (TextBox)PreviousPage.FindControl("textboxID1"); 
    txtEnddate= (TextBox)PreviousPage.FindControl("textboxID2"); 
    BindDataGrid1(txtstartdate.Text , txtEnddate.Text); 


    }