Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2010-09-24
6
Medium Priority
?
1,344 Views
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 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

0
Comment
Question by:jsvb1977
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 41

Accepted Solution

by:
guru_sami earned 2000 total points
ID: 33757649
For textboxes, you can set AutoPostBack="true" and use OnTextChanged Event like shown here:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.textbox.textchanged.aspx

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

http://quickstarts.asp.net/QuickStartv20/aspnet/doc/validation/default.aspx
0
 

Author Comment

by:jsvb1977
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.
Jason
0
 
LVL 41

Assisted Solution

by:guru_sami
guru_sami earned 2000 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
0
Monthly Recap

May was a big month for new releases from Linux Academy! Take a look at what our team built recently in our blog. You can access the newest releases from our blog.

 

Author Comment

by:jsvb1977
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?

Jason
0
 
LVL 41

Assisted Solution

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

TextBox1.Focus()
0
 

Author Closing Comment

by:jsvb1977
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.

Jason
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

722 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