I'm trying to add an event handler to a TextBox
control that is called when the text box loses focus. Is this possible?
I know WinForms has a LostFocus
event. I'm looking for something similar in ASP.NET WebForms.
You can Use OnBlur
<asp:TextBox id="TextBox1" runat="server" onblur="Javascript:alert('1234');" />
Or EDIT: Server side
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
TextBox1.Attributes.Add("onblur", "alert('hi User')");
}
}
I think you are going to have to use javascript for this and the event you will need to search and look up would be onBlur() I believe. If you need an example, I might be able to scrounge one up for you. But here are some links..
Hope these help.
This would be the blur
event, and it's a client-side event on the resulting input
element instead of a server-side event on the TextBox
control.
For example, let's say you have a TextBox
with the ID
of "TextBox1", and let's assume jQuery is included in your project template (because it probably is), then in your client-side code you might have something like this:
$('#<%= TextBox1.ClientID %>').blur(function () {
// code to respond to the event
});
This would execute any code in that function whenever the input
for that TextBox
control loses focus. Notice the use of a small block of server-side code to output the server-side control's client-side ID.