3 Kasım 2010 Çarşamba

JavaScript Kullanarak ListBox da Arama yapmak

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>SearchList</title>

</head>
<script type="text/javascript" language="javascript">
    function SearchList()
    {
        var l =  document.getElementById('<%= ListBox1.ClientID %>');
        var tb = document.getElementById('<%= TextBox1.ClientID %>');
        if(tb.value == "")
        {
            ClearSelection(l);
        }
        else{
            for (var i=0; i < l.options.length; i++)
            {
                if (l.options[i].value.toLowerCase().match(tb.value.toLowerCase()))
                {
                    l.options[i].selected = true;
                    return false;
                }
                else
                {
                    ClearSelection(l);
                }
            }
        }
    }
    function ClearSelection(lb)
    {
        lb.selectedIndex = -1;
    }

</script>
<body>
    <form id="form1" runat="server">
    <asp:TextBox ID="TextBox1" runat="server" onkeyup="return SearchList();"/><br />
    <asp:ListBox ID="ListBox1" runat="server" Height="150px" Width="250px">
    <asp:ListItem>Ali</asp:ListItem>
    <asp:ListItem>Veli</asp:ListItem>
    <asp:ListItem>Kır</asp:ListItem>
    <asp:ListItem>Dokuz</asp:ListItem>
    <asp:ListItem>Elli</asp:ListItem>
    </asp:ListBox>
    </form>
</body>
</html>

2 yorum: