关于ASP.NET中SESSION的用法

来源:百度知道 编辑:UC知道 时间:2024/06/18 07:15:15
我写了一个调用WebService的客户端,用得是C#,是查询数据库的,我想要这样的结果,先按下查询按钮,然后查询出结果,再接着按查询按钮,但是此时查询另外一张表的数据,查询出来的结果接着第一次的查询显示,而不是刷新上次的结果。具体代码我是这样写的。
public partial class _Default : System.Web.UI.Page
{
public DataTable dt;
public DataTable db;
protected void Page_Load(object sender, EventArgs e)
{

}

protected void Button1_Click(object sender, EventArgs e)
{
Service name = new Service();
string tableStr = DropDownList1.Text.ToString();
string strSql = "select name from " + tableStr;
DataTable dt = name.SQLDB(strSql).Tables[0];
Session["dt"] = dt;
try
{
DataTable db = (DataTable)Session["dt"];
GridView1.DataSource = db.DefaultView;
GridView1.DataBind();
}
catch (Exception ex)
{
throw ex;
}

1、如果采用比较笨的方法可以这样:每次点击查询都把关键字记录下来,Session["SearchKeys"]+=Session["SearchKeys"].ToString()+txtSearch.Text。然后查询语句为:
“select name from " + tableStr+" where searchKey=关键字1 or searchKey=关键字2 or",这样查询出来就是在一张表里。关键字是从Session["SearchKeys"]中提取。
2、接着你的方法,DataTable db = (DataTable)Session["dt"];
然后查询新关键字,返回DataTable db2。使用db.Merge(db2)合并两张表。