Repeater数据筛选不必重新进行数据绑定



如上页面中的数据,希望通过选择不同的筛选条件Repeater上立即显示出符合条件的数据,常规的做法是在DropDownList的SelectedIndexChanged事件处理程序中根据选中的条件拼接不同的SQL语句并进行查询,然后对Repeater重新绑定数据,今天突然想到了另一种实现思路:我们可以遍历Repeater各行,对每行中的数据进行判断,符合条件即显示该行,不符合即隐藏该行来达到同样的效果。这样做无疑会减少很多不必要的数据库查询,操作起来也非常简便。下边是一个简单的例子:

PageA.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="PageA.aspx.cs" Inherits="_PageA" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Repeater数据筛选-Mzwu.Com</title>
<style type="text/css">
<!--
body, td, th {
    font-size: 12px;
}
-->
</style>
</head>
<body>
<form id="form1" runat="server">
  <div>
    <asp:Repeater ID="Repeater1" runat="server">
      <HeaderTemplate>
        <table width="200px" border="0" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
        <tr>
          <td height="22" width="30%" align="center" valign="middle" bgcolor="#F0F0F0">姓名</td>
          <td height="22" width="70%" align="center" valign="middle" bgcolor="#F0F0F0">年龄</td>
        </tr>
      </HeaderTemplate>
      <ItemTemplate>
        <tr>
          <td height="22" width="30%" align="center" valign="middle" bgcolor="#FFFFFF"><asp:Label ID="Label1" runat="server" Text='<%# Eval("name") %>'></asp:Label></td>
          <td height="22" width="70%" align="center" valign="middle" bgcolor="#FFFFFF"><asp:Label ID="Label2" runat="server" Text='<%# Eval("age") %>'></asp:Label></td>
        </tr>
      </ItemTemplate>
      <FooterTemplate>
        </table>
      </FooterTemplate>
    </asp:Repeater>
    <br />筛选条件:
    <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
      <asp:ListItem Value="0">全部</asp:ListItem>
      <asp:ListItem Value="-1">未满18岁</asp:ListItem>
      <asp:ListItem Value="1">已满18岁</asp:ListItem>
    </asp:DropDownList>
  </div>
</form>
</body>
</html>

PageA.aspx.cs:
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _PageA : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            //数据绑定
            Repeater1_DataBind();
        }

    }

    /// <summary>
    /// 数据筛选
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        Label myLabel;
        Int32 age;
        String value = DropDownList1.SelectedValue;

        for (Int32 i = 0; i < Repeater1.Items.Count; i++)
        {
            myLabel = (Label)Repeater1.Items[i].FindControl("Label2");
            age = Convert.ToInt32(myLabel.Text);

            //未满18岁
            if (value == "-1")
            {
                if (age < 18)
                {
                    Repeater1.Items[i].Visible = true;
                }
                else
                {
                    Repeater1.Items[i].Visible = false;
                }
            }
            //已满18岁
            else if (value == "1")
            {
                if (age >= 18)
                {
                    Repeater1.Items[i].Visible = true;
                }
                else
                {
                    Repeater1.Items[i].Visible = false;
                }
            }
            //全部
            else
            {
                Repeater1.Items[i].Visible = true;
            }
        }
    }
}


评论: 0 | 引用: 0 | 查看次数: 5936
发表评论
登录后再发表评论!