RG-3
RG-3

Reputation: 6188

Textbox on TextChanged event giving error!

I have a textbox in my webpage and I want to fire as soon User clicks or enter something on this textbox. Code is working but it is firing when an User hits Enter button. I want it to fire when user types their 1st character. Which event is app for this?

This is my mockup code:

<asp:TextBox ID="txtAgentName" runat="server" OnTextChanged = "txtAgentName_TextChanged"></asp:TextBox>

But when I am running this webpage it is showing me this err:

CS0123: No overload for 'txtAgentName_TextChanged' matches delegate 'System.EventHandler'

I have this on my code behind:

  protected virtual void txtAgentName_TextChanged(object sender, EventArgs e)
    {

    }

Made a breakpoint in in here, and seems like it is firing when I am hitting Enter button. I want to fire when user enter 1st character inside that textbox.

What I am doing wrong here?

Upvotes: 0

Views: 2483

Answers (3)

jayesh
jayesh

Reputation: 2492

<%@ Page Language="C#" %>
<script runat="server">
    protected void txtSearch_TextChanged(object sender, EventArgs e)
    {
        lblSearchResults.Text = "Search for: " + txtSearch.Text;
    }
</script>
<html>
<head>
    <title>TextBox AutoPostBack</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

    <asp:Label
        id="lblSearch"
        Text="Search:"
        Runat="server" />
    <asp:TextBox
        id="txtSearch"
        AutoPostBack="true"
        OnTextChanged="txtSearch_TextChanged"
        Runat="server" />

    <hr />

    <asp:Label
        id="lblSearchResults"
        Runat="server" />

    </div>
    </form>
</body>
</html>

Upvotes: 1

Priyank
Priyank

Reputation: 10623

Use javascript's TextBox1.Attributes.Add("OnKeyPress", "GetKeyPress()") and then postback if you wish.

Upvotes: 1

Bala R
Bala R

Reputation: 108957

your event handler must have this signature

protected void txtAgentName_TextChanged(object sender, EventArgs e)
{
   ...
}

does it?

Upvotes: 2

Related Questions