。net 之view筛选

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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;
    }
}

  

posted @   尘梦  阅读(184)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示
主题色彩