GridView高效分页和搜索功能的实现代码(5)

7、PagerBtnCommand_OnClick:该方法主要用来处理设计视图页的“首页”、“下一页”,“上一页”,“尾页”,“查询”按钮的Click事件,主要通过不同按钮的CommandName属性来分别处理,需要在前台为每一个按钮相应的CommandName属性赋值,如果用户点击的是“查询”按钮,这个时候需要对查询的Sql语句进行重写,加入过滤条件,即用户输入的查询的条件

复制代码 代码如下:


/// <summary>
        /// 页面按钮Click处理
        /// </summary>
        /// <param></param>
        /// <param></param>
        protected void PagerBtnCommand_OnClick(object sender, EventArgs e)
        {
            CurrentPage = (int)ViewState["PageIndex"];
            //从ViewState中读取页码值保存到CurrentPage变量中进行参数运算
            Pages = (int)ViewState["PagesCount"];//从ViewState中读取总页参数运算
            Button btn = sender as Button;
            string sqlResult="default";
            if (btn != null)
            {
                string cmd = btn.CommandName;
                switch (cmd)//根据不同的CommandName做出不同的处理
                {
                    case "next":
                        CurrentPage++;
                        break;
                    case "prev":
                        CurrentPage--;
                        break;
                    case "last":
                        CurrentPage = Pages;
                        break;
                    case "search":
                        if (!string.IsNullOrEmpty(SearchName.Text))
                        {
                            RecordsCount = GetRecordsCount("select count(*) from p_user where username like '" + SearchName.Text + "%'");//获取过滤后的总记录数
                            PagesCount = RecordsCount / PAGESIZE + OverPage();//该变量为页总数
                            ViewState["PagesCount"] = RecordsCount / PAGESIZE - ModPage();//该变量为末页索引,比页总数小1
                            ViewState["PageIndex"] = 0;//保存一个为0的页面索引值到ViewState,页面索引从0开始
                            ViewState["JumpPages"] = PagesCount;
                            //保存PageCount到ViewState,跳页时判断用户输入数是否超出页码范围
                            //显示lbPageCount、lbRecordCount的状态
                            lbPageCount.Text = PagesCount.ToString();
                            lbRecordCount.Text = RecordsCount.ToString();
                            //判断跳页文本框失效
                            if (RecordsCount <= 10)
                                GotoPage.Enabled = false;
                sqlResult = "Select Top " + PAGESIZE + "user_serialid,username,displayname,orgcode,orgname from p_user where user_serialid not in(select top " + PAGESIZE * CurrentPage + " user_serialid from p_user order by user_serialid asc) and username like '" + SearchName.Text + "%' order by user_serialid asc";
                        }
                        else
                        {
                            Response.Write("请输入您所要查找的用户姓名!");
                        }
                        break;
                    case "jump":
                       JumpPage = (int)ViewState["JumpPages"];
                      //从ViewState中读取可用页数值保存到JumpPage变量中
                      //判断用户输入值是否超过可用页数范围值
                      if(Int32.Parse(GotoPage.Text) > JumpPage ||      Int32.Parse(GotoPage.Text) <= 0)
Response.Write("<script>alert('页码范围越界!')</script>");
                      else
                      {
                          int InputPage = Int32.Parse(GotoPage.Text.ToString()) - 1;
                          //转换用户输入值保存在int型InputPage变量中
                          ViewState["PageIndex"] = InputPage;
                          CurrentPage = InputPage;
                         //写入InputPage值到ViewState["PageIndex"]中
                          sqlResult = "Select Top " + PAGESIZE + "user_serialid,username,displayname,orgcode,orgname from p_user where user_serialid not in(select top " + PAGESIZE * CurrentPage + " user_serialid from p_user order by user_serialid asc) and username like '" + SearchName.Text + "%' order by user_serialid asc";
                      }
                        break;
                    default:
                        CurrentPage = 0;
                        break;
                }
                ViewState["PageIndex"] = CurrentPage;
                //将运算后的CurrentPage变量再次保存至ViewState
                GridViewDataBind(sqlResult);//调用数据绑定函数TDataBind()
            }
        }

8、btn_Reset_Click:该方法主要用来进行重置,用户完成一次查询之后,需要重置,才能进行下一次的查询操作

复制代码 代码如下:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wjdyfj.html