使用session在不同頁面之間傳遞參數,sqldatasource按條件查詢

1.如圖1 所示用戶設置預警參數,通過session將textbox輸入的參數傳入另一個頁面,如圖2所示:


圖1

2.圖1的後臺代碼如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class WebForm19 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Session["Temp1"] = TextBox1.Text;
            Session["Temp2"] = TextBox2.Text;
            Session["Humid1"] = TextBox3.Text;
            Session["Humid2"] = TextBox4.Text;
            Session["Deform1"] = TextBox5.Text;
            Session["Deform2"] = TextBox6.Text;
        }

        protected void TextBox1_TextChanged(object sender, EventArgs e)
        {

        }

        protected void Button4_Click(object sender, EventArgs e)
        {
            Session["Temp1"] = TextBox1.Text;
            Session["Temp2"] = TextBox2.Text;
            Session["Humid1"] = TextBox3.Text;
            Session["Humid2"] = TextBox4.Text;
            Session["Deform1"] = TextBox5.Text;
            Session["Deform2"] = TextBox6.Text;
        }
    }
}
3.如圖2所示,左側顯示圖1傳入的預警參數,右側通過添加數據源,選擇查詢的範圍(在預警參數範圍內),病通過Gridview控件顯示在表格中


圖2

4.圖2的後臺代碼如下所示:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
using System.Data;
using System.Data.SqlClient;

namespace WebApplication1
{
    public partial class WebForm15 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //若管理員不設置預警參數,使用系統默認預警參數
            if (Session["Temp1"] == null || Session["Temp2"] == null || Session["Humid1"] == null || Session["Humid2"] == null || Session["Deform1"] == null || Session["Deform2"] == null)
            {
                Label3.Text = Convert.ToString(15);
                Label4.Text = Convert.ToString(25);
                Label5.Text = Convert.ToString(40);
                Label6.Text = Convert.ToString(60);
                Label7.Text = Convert.ToString(4);
                Label8.Text = Convert.ToString(9);
            }
            else
            {
                //顯示WarningParameter1.aspx設置的預警參數
                Label3.Text = Session["Temp1"].ToString();
                Label4.Text = Session["Temp2"].ToString();
                Label5.Text = Session["Humid1"].ToString();
                Label6.Text = Session["Humid2"].ToString();
                Label7.Text = Session["Deform1"].ToString();
                Label8.Text = Session["Deform2"].ToString();
                //根據預警參數篩選異常數據
            }

        }

       

        protected void GridView2_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}


5.sqldatasource按條件查詢

在Gridview點擊新建數據源,選擇數據庫,選擇數據表,選擇數據列,添加where語句



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章