2016-03-01 87 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); 


    }