Asp.net技术点------GridView+AspnetPager+Linq+ViewState 处理数据条件问题

本文介绍了一个使用ASP.NET实现的简单查询条件保存及分页显示功能的方法。通过将查询条件保存到ViewState中,使得用户在进行翻页操作时能够保留之前的搜索条件,从而避免重复输入。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天第一次发布文章解决

 

查询条件保存:

 

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

public partial class UserManager : System.Web.UI.Page
{

//调用获取 数据方法
MapUni.BLL.BLL.Test_tblSalesPerson bll = new MapUni.BLL.BLL.Test_tblSalesPerson();

//定义ViewState的key值
const string vsKey = "searchCriteria";

///<summary>
/// 界面初始化
///</summary>
///<param name="sender"></param>
///<param name="e"></param>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//界面初始化绑定数据
Bind_Grid(string.Empty);
}
}

///<summary>
/// 绑定数据
///</summary>
///<param name="str_where"></param>
public void Bind_Grid(string str_where)
{
//进行数据 判断
if (str_where == null)
{
str_where = "";
}
//获取list数据
var Test_DataList = bll.GetModelList(str_where);

//设置分页控件的记录总数
AspNetPager1.RecordCount = Test_DataList.Count();

//linq处理数据数显示
var qureys =( from m in Test_DataList select m).Skip(AspNetPager1.StartRecordIndex-1).Take(AspNetPager1.PageSize);

//数据绑定
this.GridView1.DataSource = qureys;
this.GridView1.DataBind();
}

///<summary>
/// 点击查询方法
///</summary>
///<param name="sender"></param>
///<param name="e"></param>
protected void btnSelect_Click(object sender, EventArgs e)
{
      //设置查询为第一页显示
AspNetPager1.CurrentPageIndex = 1;

//判断用户名是否为空
if (this.txt_UserName.Text.Length != 0)
{

//带入查询条件
string where = " UserName like '%" + this.txt_UserName.Text + "%'";

//保存条件到ViewState
ViewState[vsKey] = where;
//进行数据绑定
Bind_Grid(where);
}
else
{
ViewState[vsKey] = null;
//进行数据绑定
Bind_Grid(string.Empty);

}
}


///<summary>
/// 点击翻页控件查询
///</summary>
///<param name="sender"></param>
///<param name="e"></param>

protected void AspNetPager1_PageChanged1(object sender, EventArgs e)
{
//获取查询条件
Bind_Grid((string)ViewState[vsKey]);
}
}



转载于:https://www.cnblogs.com/Maorj19880818/archive/2011/11/22/mars19880818.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值