。net 之view筛选

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

public partial class CACHE查询条件 : System.Web.UI.Page
{
   
    protected void Button1_Click(object sender, EventArgs e)
    {
        string cond = "";
        if (this.DropDownList1.SelectedIndex > 0)
        {
             cond += "stu_sex='" + this.DropDownList1.SelectedValue + "'";
          
        }
        else
        {
             cond += "1=1";   
        }

        if (this.TextBox1.Text != "")
        {
            cond += " and age=" + this.TextBox1.Text;
        }
        ViewState["cond"] = cond;
        Page_Load(null, null);
    }
    //页面加载时,将DataSet插入到委托中去。
    protected void Page_Load(object sender, EventArgs e)
    {

        //首先从Cache中获取Dataset
        DataSet students = (DataSet)Cache["Students"];
        //如果Cache中不存在DataSet,则从数据库中获取DataSet,并添加到缓存中
        if (students == null)
        {
            students = GetStudentDataSet();


      
            //创建一个基于web.config配置文件的依赖,当该文件发生变化时,则从缓存中移除缓存项。
            CacheDependency cd = new CacheDependency(Server.MapPath("web.config"));

            //在Insert中指定该依赖项,并且指定缓存的优先级为高优先级,传递一个传托。
            Cache.Insert("Students", students, cd, System.Web.Caching.Cache.NoAbsoluteExpiration, TimeSpan.FromHours(1));
        }

        DataView view = students.Tables[0].DefaultView;
        if (ViewState["cond"] != null)
        {
            view.RowFilter = ViewState["cond"].ToString();
        }
        GridView1.DataSource = view;
        //GridView1.DataSource = students;

        GridView1.DataBind();
    }
    /// <summary>
    /// 从数据库中获取产品信息,并返回一个DataSet对象
    /// </summary>
    /// <returns></returns>
    protected DataSet GetStudentDataSet()
    {
        string sql = "select * from t_student";
        DataSet ds = DBHelper.SqlHelper.ExecuteDataSetText(sql, null);
        return ds;
    }
}

  

原文地址:https://www.cnblogs.com/mengluo/p/6078295.html