How to run a code-behind sub from the 'blur' event in an input text box

Posted on 2010-09-24
Last Modified: 2013-11-26
I am developing a form where I need to validate some of the information the user enters. I would like to validate this information AS THE USER moves through the form.

So, i was thinking that I could call SUBS and/or FUNCTIONS using the OnBlur Event on the form fields I need to validate.

I am not sure how to do this. I really would like to be able to call some code-behind routines instead of using javascript. In fact, JS is out of the question for the routines themselves, but I would be willing to use it to call the routines if need be.

Is this even possible?

You will see in the code example below that i am using an asp:image button that fires the validation routine using the onclick event. I would like to apply that same logic the form's input "OnBlur" event.

Thank you in advance for your reply.
<label for="txbCustomerCode">Customer Code: </label>
        <input id="txbCustomerCode" type="text" size="15" name="txbCustomerCode" runat="server" tabindex="3" title="Your Four Season's account number. You can find this number on an invoice, or contact your Sales Representative." />
        <asp:ImageButton CssClass="validation" ImageUrl="Images/Warning-20.png" runat="server" OnClick="subCheckCustomerCode" ID="imgCustomerCodeValidate" />
        <p class="labelinfo">Your Four Season's account number. You can find this number on an invoice, or contact your Sales Representative.</p>
        <br class="clear" />


    Protected Sub subCheckCustomerCode(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles imgCustomerCodeValidate.Click
        Dim strCustomerCode As String = "123456"
        Dim strCustomerCodeText As String = txbCustomerCode.Value

        If strCustomerCodeText = strCustomerCode Then
            imgCustomerCodeValidate.Attributes("src") = "Images/Checkmark-20.png"
            imgCustomerCodeValidate.Attributes("src") = "Images/Error-20.png"
        End If

    End Sub

Open in new window

Question by:jsvb1977
  • 3
  • 3
LVL 41

Accepted Solution

guru_sami earned 500 total points
ID: 33757649
For textboxes, you can set AutoPostBack="true" and use OnTextChanged Event like shown here:

Did you look into using Validation controls? They might be of help to you depending on what kind of validation you are looking for:

Author Comment

ID: 33757806
This might work, thank you. I am heading off for the weekend but will attempt to integrate the validation on Monday.

I will say that I did attempt to use the AutoPostBack as well as the OnTextChanged without success.
I was expecting the code to run as the user 'Tabbed Out' of the asp:textbox.

Does the ontextchanged only fire when the entire form is submitted or can it operate as what I have been referring to as "OnBlur" or "Lost_Focus" event?

I can't post the code yet because I have SQL Connections and Passwords and things like that integrated into the OnTextChanged Sub.

I will strip all of that out and post what I have Monday Morning.

Thank you so much for the links. I think I am now headed in the correct direction.
LVL 41

Assisted Solution

guru_sami earned 500 total points
ID: 33757827
This is the key part:

-->I will say that I did attempt to use the AutoPostBack as well as the OnTextChanged without success.
I was expecting the code to run as the user 'Tabbed Out' of the asp:textbox.

Yes, it should if the text changes. But if you leave blank, I think it might not.

Here is the basic stub you need after setting autopostback="true"

Protected Sub TextBox1_TextChanged(ByVal sender As Object, _
        ByVal e As System.EventArgs) Handles TextBox1.TextChanged
   ' you validation code goes here
End Sub
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.


Author Comment

ID: 33757885
Yes, it is working now.

Very Cool! Exactly the effect I was looking for. On thing, though...

I lose focus on the form as the page loads again. So, for example, I have this particular textbox with a tabindex of 3. When the input is validated in the code behind and the page loads again, it would be logical for the focus to be on the next textbox with tabindex of 4.

Know what I mean?

I thought MaintainScrollPositionOnPostback="true" would help but it does not. Any thoughts?
will I need to programmatically assign the focus on the next tabindex from the routine in the code behind?

LVL 41

Assisted Solution

guru_sami earned 500 total points
ID: 33757952
one way is to set the focus of that textbox in the textchanged handler like:


Author Closing Comment

ID: 33772628
This Expert went above and beyond. I always get concerned when the first response is links to MSDN articles, but this Expert immediately followed up when I had additional questions and then took the time to answer a follow up question I had not thought of at first.

The solution was thorough and accurate.


Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Authentication of Web Services 3 53
Not showing JavaScript in the list 5 39
Error in JQuery 5 39
Crystal reports 2 16
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now