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


    今天第一次发布文章解决

    查询条件保存:

    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]);
    }
    }



  • 相关阅读:
    objective-c内存管理中autorelease的作用
    objective-c在Xcode中@property相关参数的解释
    objective-c中的内存管理
    c#扩展方法-摘自msdn
    objective-c中的category
    c语言中结构体的定义、初始化及内存分配
    c语言中的结构体为值类型,当把一个结构体赋值给另一个结构体时,为值传递
    手动通过Lucene判断该pom文件中jar是否存在,子依赖没判断
    代码方式删除SVN
    Maven多层嵌套
  • 原文地址:https://www.cnblogs.com/Maorj19880818/p/mars19880818.html
Copyright © 2020-2023  润新知