This solution is based on Ktash's answer. I did this because I want to learn more about javascript, DOM, and RegExp.
I changed the "keypress" event from "keydown" since the previous one does not fire when backspace / delete, so deleting all characters with backspace / delete is still in the list.
[Default.aspx]
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="RealtimeCheckBoxListFiltering.Default" %> <!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 runat="server"> <title></title> <script type="text/javascript"> window.onload = function () { var tmr = false; var labels = document.getElementById('cblItem').getElementsByTagName('label'); var func = function () { if (tmr) clearTimeout(tmr); tmr = setTimeout(function () { var regx = new RegExp(document.getElementById('inputSearch').value); for (var i = 0, size = labels.length; i < size; i++) if (document.getElementById('inputSearch').value.length > 0) { if (labels[i].textContent.match(regx)) setItemVisibility(i, true); else setItemVisibility(i, false); } else setItemVisibility(i, true); }, 500); function setItemVisibility(position, visible) { if (visible) { labels[position].style.display = ''; labels[position].previousSibling.style.display = ''; if (labels[position].nextSibling != null) labels[position].nextSibling.style.display = ''; } else { labels[position].style.display = 'none'; labels[position].previousSibling.style.display = 'none'; if (labels[position].nextSibling != null) labels[position].nextSibling.style.display = 'none'; } } } if (document.attachEvent) document.getElementById('inputSearch').attachEvent('onkeypress', func); </script> </head> <body> <form id="form1" runat="server"> <table> <tr> <td> <asp:TextBox runat="server" ID="inputSearch" ClientIDMode="Static" /> </td> </tr> <tr> <td> <asp:CheckBoxList runat="server" ID="cblItem" ClientIDMode="Static" RepeatLayout="Flow" /> </td> </tr> </table> </form> </body> </html>
[Default.aspx.cs]
using System; using System.Collections.Generic; namespace RealtimeCheckBoxListFiltering { public partial class Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { cblItem.DataSource = new List<string>() { "qwe", "asd", "zxc", "qaz", "wsx", "edc", "qsc", "esz" }; cblItem.DataBind(); } } }
source share