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:L开发者_Python百科abel>
</ContentTemplate>
</ajax:UpdatePanel>
But when I am typing in the TextBox. lblMessage
is not updating.
How to rectify this?
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.
精彩评论