将数据网格视图从用户提交发送到新页面
本文关键字:提交 新页面 用户 数据 数据网 网格 视图 | 更新日期: 2023-09-27 18:36:53
我有一个Web表单,用户在其中使用jQuery DatePicker提交日期范围。然后,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);
}
当用户输入日期时,数据网格视图将显示在同一页上。有没有办法在提交后在新 Web 窗体上显示数据网格视图?
我尝试将重定向添加到新页面的按钮,并在新页面上为 datagridview 建立 html 代码,但我似乎无法正确,因为它是一种新形式。
我对你的代码做了一些更改。通过使用 DataGrid1(string FirstDate, string SecondDate)
,您需要返回 DataTable
作为结果并将此结果存储到 session
中。例如
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并将以下代码写入代码隐藏
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt= (DataTable)Session["MyGridData"];
if (dt.Rows.Count > 0)
{
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
我只会创建另一个带有 DataGrid 的页面,并在单击按钮时将日期参数作为查询字符串传递给它。
喜欢这个:
带有按钮和日期选择器的页面:
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect(string.Format("MyDataGridPage.aspx?firstDate={0}&secondDate={1}", TextFirstDate.Text, TextSecondDate.Text));
}
数据网格页:
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);
}
}
你应该使用跨页帖子。在按钮中输入这样的回发网址
在发布的页面中,您可以在 PageLoad 事件中获取像这样输入的日期值受保护的无效Page_Load(对象发送器,事件参数 e) { 文本框 txtstartdate; 文本框 txt结束日期;
//getting controls from previous page
txtstartdate= (TextBox)PreviousPage.FindControl("textboxID1");
txtEnddate= (TextBox)PreviousPage.FindControl("textboxID2");
BindDataGrid1(txtstartdate.Text , txtEnddate.Text);
}