Rajkumar Gor
Rajkumar Gor

Reputation: 57

How to use 'onKeyUp' attribute in asp.net?

Message 1: Validation (ASP.Net): Attribute 'onkeyup' is not a valid attribute of element 'TextBox'. E:\ABC\ABCD.aspx 83 66 ABCD

I want dropdown to change the Display list according to the value typed in TextBox.I have written the following query for it

var ddlText, ddlValue, ddl, lblMesg;

function CacheItems() {
    ddlText = new Array();
    ddlValue = new Array();
    ddl = document.getElementById("=STCDropDownList.ClientID");
    lblMesg = document.getElementById("=Label1.ClientID");

    for (var i = 0; i < ddl.options.length; i++) {
        ddlText[ddlText.length] = ddl.options[i].text;
        ddlValue[ddlValue.length] = ddl.options[i].value;
    }
}

window.onload = CacheItems;

function FilterItems(value) {
    ddl.options.length = 0;
    for (var i = 0; i < ddlText.length; i++) {
        if (ddlText[i].toLowerCase().indexOf(value) != -1) {
            AddItem(ddlText[i], ddlValue[i]);
        }
    }

    lblMesg.innerHTML = ddl.options.length + " item(s) found.";

    if (ddl.options.length == 0) {
        AddItem("No item found.", "");
    }
}

function AddItem(text, value) {
    var opt = document.createElement("option");
    opt.text = text;
    opt.value = value;
    ddl.options.add(opt);
}

The below following are Textbox and DropDownlist i am using.

        <asp:TextBox ID="STCTextBox" runat="server"  onkeyup="FilterItems(this.value)"></asp:TextBox>
    </td>
</tr>
<tr>
    <td class="auto-style3">Service Tax Code</td>
    <td class="auto-style3">
        <asp:DropDownList ID="STCDropDownList" runat="server" AutoPostBack="True" DataSourceID="STCSqlDataSource" DataTextField="ServiceTaxCode" DataValueField="ServiceTaxCode"></asp:DropDownList>
        <asp:Label ID="Label1" runat="server" Text="Entered Comm Code Already Registered" Visible="False"></asp:Label>

Upvotes: 0

Views: 1131

Answers (3)

rp4361
rp4361

Reputation: 445

Rather use jQuery:

$("#id").keyup(function(){<br/>
        // Your Code <br/>
});

Upvotes: 0

Riddler
Riddler

Reputation: 576

From What i see

Replacing Function FilterItems with this can help

var txtBox=document.getElementById("=STCTextBox.ClientID");

txtBox.onkeyup=function() {
var value=txtBox.value;

    ddl.options.length = 0;
    for (var i = 0; i < ddlText.length; i++) {
        if (ddlText[i].toLowerCase().indexOf(value) != -1) {
            AddItem(ddlText[i], ddlValue[i]);
        }
    }

    lblMesg.innerHTML = ddl.options.length + " item(s) found.";

    if (ddl.options.length == 0) {
        AddItem("No item found.", "");
    }
}

Upvotes: 0

ThePravinDeshmukh
ThePravinDeshmukh

Reputation: 1913

Simple jQuery Function

.keyup()

Explore https://api.jquery.com/keyup/

Upvotes: 1

Related Questions