Link to home
Start Free TrialLog in
Avatar of jsvb1977
jsvb1977

asked on

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

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 vb.net 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"
        Else
            imgCustomerCodeValidate.Attributes("src") = "Images/Error-20.png"
        End If

    End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of guru_sami
guru_sami
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jsvb1977
jsvb1977

ASKER

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.
Jason
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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?

Jason
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.

Jason