Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OnTextChanged event is not firing

I have a TextBox inside my .Aspx page:

<ajax:UpdatePanel ID="UpdatePanel2" runat="server">
    <ContentTemplate>
        <asp:TextBox ID="txtCity" AutoPostBack="true" OnTextChanged="txtCity_TextChanged"
            Width="90%" runat="server" ></asp:TextBox>
    </ContentTemplate>
</ajax:UpdatePanel>

Code behind:

protected void txtCity_TextChanged(object sender, EventArgs e)
{
    lblMessage.Text = "you have typed:" + txtCity.Text;
}

And for lblMessage [on the same .Aspx page]:

<ajax:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
    <ContentTemplate>
        <asp:Label ID="lblMessage" runat="server" Text="" ></asp:Label>
    </ContentTemplate>
</ajax:UpdatePanel>

But when I am typing in the TextBox. lblMessage is not updating.

How to rectify this?

like image 261
Chris Avatar asked Apr 11 '11 16:04

Chris


1 Answers

It sounds like you're thinking that the OnTextChange event is fired while you are typing in the text box. This is not true. OnTextChange is a server-side event and only fires when the page (or panel) is posted back. Typing into a text box on a page does not post the page back and so this event will only fire once you submit the form.

What you would actually want to do in this case, is to use some JavaScript with the onkeypress JavaScript event to update the label text as things are typed into the TextBox. JavaScript is run on the client and doesn't require you to post back the page in order for it to run.

like image 112
Richard Marskell - Drackir Avatar answered Sep 22 '22 14:09

Richard Marskell - Drackir