【接上期】
目录
作为数据类的GridView控件,数据量大了以后,仅仅实现翻页是不行的,还需要用到关键字查找、条件过滤等手段,方便用户的实际使用。
本期主要实现针对“姓名”关键字查找(支持模糊查询)的案例。
一、界面修改
1.添加布局表格
上期我们给表格顶部添加一个“信息栏”,使用表格进行排版,这次要添加用于查询的控件,为了美观需要在表格内再添加一列。
- <table class="table table-bordered" width="100%" style="margin-bottom:0px">
-
- <tbody>
-
- <tr>
-
- <td>记录总数:<asp:Label ID="labTotal" runat="server" Text="<%# Total %>"></asp:Label></td>
-
- <td>按姓名搜素: </tr>
-
- </tbody>
-
- </table>
2.添加控件
在新的单元格内添加TextBox、Button两个控件,ID分别为:txtSearch、btnSearch。
3.控件美化
- <table class="table table-bordered" width="100%" style="margin-bottom:0px">
- <tbody>
- <tr>
- <td class = "align-middle" style="border-right:0">记录总数:<asp:Label ID="labTotal" runat="server" Text="<%# Total %>"></asp:Label></td>
- <td class=" justify-content-end form-inline" style="border-left:0" >
-
- 按姓名搜素:
- <asp:TextBox ID="txtSearch" runat="server" CssClass="form-text text-left align-middle m-0" Width="68" />
- <asp:Button ID="btnSearch" runat="server" Width="58px" CssClass="btn btn-primary btn-sm m-1" Text="查找"/>
-
- </td>
- </tr>
- </tbody>
- </table>
页面效果:

二、编写代码
1.编写按钮点击事件的处理代码
双击查找按钮添加单击事件处理,代码为:
- protected void btnSearch_Click(object sender, EventArgs e)
- {
- if (this.txtSearch.Text.Trim() != String.Empty)
- {
- this.SqlDataSource1.SelectCommand = "SELECT * FROM [qxmm_demo] WHERE xm like '%" + this.txtSearch.Text + "%'";
- }
- else
- {
- this.SqlDataSource1.SelectCommand = "SELECT * FROM [qxmm_demo]";
- }
- //启用分页(如果上次查询数量较少不足一页,分页会被关闭,此时GridView1.PageCount的值总是1
- //这里启用防止出现超过一页
- this.GridView1.AllowPaging = true;
- //刷新Gridview控件
- this.GridView1.DataBind();
- //再次计算总记录数
- GetRecordTotal();
- }
上述代码主要是修改SqlDataSource1的查询命令,改成带有模糊查询的SQL语句
SELECT * FROM [qxmm_demo] WHERE xm like '%" + this.txtSearch.Text + "%'
需要注意的是,SqlDataSource1的查询命令修改后,需要用GridView1.DataBind();刷新一下Gridview控件。此外还要开启“分页”模式(GridView1.AllowPaging = true; )因为在“是否显示分页控件”代码中,会获根据总页数来判断是否显示分页空间控件,如果上一次检索时关闭了分页模式(返回数据小于一页),再获取全部数据时,GridView1.PageCount就会始终保持“1”,这样就GridView就不在分页显示了。
最后,再调用RecordTotal()重新计算一次总记录数
2.注释掉“尾页填充空行”中无用的代码
在填充尾页空行的AddEmptyToFullPage过程中,需要注释掉GridView.BottomPagerRow.Visible = true。因为我们分页控件的显示控制采用了直接对GridView的AllowPaging属性赋真/假值的办法,因此就不需要了。

三、解决查不到数据的显示
1.故障现象
当查不到数据时,页面会出现如下情形:

2.解决思路
之所以发生这种情况,是因为SqlDataSource1中没有查到记录,记录总数为0,这是GridView没有可渲染的数据。
按照这个思路,我们在代码中给他换个数据源,在数据源里加上“查无此人”的提示,问题就可以解决了。
3.编写代码
在查询按钮的单击事件(btnSearch_Click)处理中,我们可以检测数据总数,如果是0则更换数据源:
- //再次计算总记录数
- GetRecordTotal();
- //如果总数为0
- if (this.Total == 0)
- {
- //提示没找到
- //使用Web.congfig内的数据连接字符串
- string connectionString = ConfigurationManager.ConnectionStrings["misConnectionString"].ConnectionString;
- //查询字符串
- string query = this.SqlDataSource1.SelectCommand;
- //建立DataSet对象
- DataSet dataSet = GetData(connectionString, query);
- //添加一列
- dataSet.Tables[0].Columns.Add(new DataColumn("columns", typeof(string)));
- // 创建新行
- DataRow newRow = dataSet.Tables[0].NewRow();
- // 设置行的值
- newRow["xm"] = "查无此人";
- // 将新行添加到表
- dataSet.Tables[0].Rows.Add(newRow);
- //给Gridview更换数据源
- this.GridView1.DataSourceID = null;
- this.GridView1.DataSource = dataSet;
- }
- else
- {
- //如果有数据,需要把数据源跟换回来,否则GridView还是无法渲染
- this.GridView1.DataSourceID = this.SqlDataSource1.ID;
- }
修改完成:

四、最终效果
1.普通查询

2.模糊查询

3.无关键字查询:

4.查不到记录

文章结束。创作图文不易,感谢点赞支持,欢迎收藏、关注我后期文章。