[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Set focus to a text box

Posted on 2009-04-22
24
Medium Priority
?
920 Views
Last Modified: 2012-06-27
Hi Guys! I am in the midst of developing an HRIS for my company. I'm stuck in a situation where once I enter the value in the empid field, before I go to the next field and enter the empname, it should alert me that the empid exists and the focus should remain on empid field to enter a unique empid. Only after entering a unique empid, it should allow me to go to the next field. I was wondering if there is a way to enter the code when empname textbox gets focus. It should validate, if unique empid then continue else return to empid field. Or is there any other way to accomplish this. Thanks in advance.
Dim con As New System.Data.OleDb.OleDbConnection
        Dim dreader As System.Data.OleDb.OleDbDataReader
        Dim myCommand As New System.Data.OleDb.OleDbCommand
        Dim myPath As String
 
        myPath = Server.MapPath("hayat.mdb")
        con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data source=" & myPath & ";"
        myCommand.CommandText = "select * from empdetails where empid = " & txtempid.Text & ""
        myCommand.Connection = con
        con.Open()
        dreader = myCommand.ExecuteReader
 
        While dreader.Read()
            txtempidcheck.Text = dreader(1)
            If (txtempidcheck.Text) = (txtempid.Text) Then
                lblmsg.Text = "Please enter a unique id"
            End If
        End While
 
        dreader.Close()
        con.Close()

Open in new window

0
Comment
Question by:preethamonline
  • 11
  • 9
  • 4
24 Comments
 
LVL 1

Expert Comment

by:MarcGraff
ID: 24210715
Assuming your checking against a DB; you could set an on blur event on your empID field to validate using AJAX to return to the server and check the ID. This would take a split second though...
0
 
LVL 1

Expert Comment

by:MarcGraff
ID: 24210726
Or you could validate against a web service which would probably be more efficient.
0
 
LVL 1

Expert Comment

by:MarcGraff
ID: 24210762
Here is an example of the page:
<%@ Page Language="VB" MasterPageFile="~/_StandardPage.master" AutoEventWireup="false"
    CodeFile="Test2.aspx.vb" Inherits="Test" Title="Untitled Page" %>
<%@ MasterType VirtualPath="~/_StandardPage.master" %>
 
<%@ Register Src="~/Test/UDV_Check.ascx" TagName="UDV_Check" TagPrefix="uc1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="CPH_Main" Runat="Server">
    <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>			
    
    <table>
        <tr>
            <td>
                Emp ID:
            </td>
            <td>
                <asp:TextBox ID="TXT_EmpID" runat="server" onblur="$get('ctl00$CPH_Main$BTN_Check').click();"></asp:TextBox>
            </td>
            <td>
                <asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">
                    <ContentTemplate>
                        <asp:Label ID="LBL_Error" runat="server" Text=""></asp:Label>
                    </ContentTemplate>
                    <Triggers>
                        <asp:AsyncPostBackTrigger ControlID="BTN_Check" EventName="Click" />
                    </Triggers>
                </asp:UpdatePanel>
                <asp:Button ID="BTN_Check" runat="server" Style="visibility: hidden;" />
            </td>
        </tr>
        <tr>
            <td>
                Emp Name:
            </td>
            <td>
                <asp:TextBox ID="TXT_EmpName" runat="server"></asp:TextBox>
            </td>
            <td>
                
            </td>
        </tr>
    </table>
</asp:Content>

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Expert Comment

by:MarcGraff
ID: 24210765
and the codebehind

Partial Class Test
    Inherits System.Web.UI.Page
 
    Protected Sub BTN_Check_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BTN_Check.Click
 
        ' Do your validation
 
        If TXT_EmpID.Text = "12345" Then
            LBL_Error.Text = "This is a duplicate EmpID"
        Else
            LBL_Error.Text = ""
        End If
    End Sub
End Class

Open in new window

0
 
LVL 1

Expert Comment

by:MarcGraff
ID: 24210772
Oops forgot the focus
Partial Class Test
    Inherits System.Web.UI.Page
 
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Master.Secure = False
    End Sub
 
    Protected Sub BTN_Check_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BTN_Check.Click
 
        ' Do your validation
 
        If TXT_EmpID.Text = "12345" Then
            LBL_Error.Text = "This is a duplicate EmpID"
            ScriptManager.RegisterStartupScript(Me.Page, Me.GetType, "SetFocus", "$get('" & TXT_EmpID.ClientID & "').focus();", True)
        Else
            LBL_Error.Text = ""
        End If
    End Sub
End Class

Open in new window

0
 

Author Comment

by:preethamonline
ID: 24212461
Thanks Macgraff. Will try this out and let you know the result.
0
 

Author Comment

by:preethamonline
ID: 24212940
Hi Macgraff, i am finding it very difficult to comprehend the code. Could you please simplify it a bit. Is there any other simpler way of doing it.
0
 
LVL 1

Expert Comment

by:MarcGraff
ID: 24214886
Hmm, what exactly are you having a problem with?
0
 
LVL 1

Expert Comment

by:MarcGraff
ID: 24214939
Try this... This one does not use a master page.

Attached is the aspx
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Test3.aspx.vb" Inherits="Test3" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <table>
            <tr>
                <td>
                    Emp ID:
                </td>
                <td>
                    <asp:TextBox ID="TXT_EmpID" runat="server"></asp:TextBox>
                </td>
                <td>
                    <asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">
                        <ContentTemplate>
                            <asp:Label ID="LBL_Error" runat="server" Text=""></asp:Label>
                        </ContentTemplate>
                        <Triggers>
                            <asp:AsyncPostBackTrigger ControlID="BTN_Check" EventName="Click" />
                        </Triggers>
                    </asp:UpdatePanel>
                </td>
            </tr>
            <tr>
                <td>
                    Emp Name:
                </td>
                <td>
                    <asp:TextBox ID="TXT_EmpName" runat="server"></asp:TextBox>
                </td>
                <td>
                </td>
            </tr>
        </table>
        <asp:Button ID="BTN_Check" runat="server" Style="visibility: hidden;" />
    </form>
</body>
</html>

Open in new window

0
 
LVL 1

Accepted Solution

by:
MarcGraff earned 1400 total points
ID: 24214941
And the code behind:
Partial Class Test3
    Inherits System.Web.UI.Page
 
    Protected Sub BTN_Check_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BTN_Check.Click
 
        ' Do your validation
 
        If TXT_EmpID.Text = "12345" Then
            LBL_Error.Text = "This is a duplicate EmpID"
            ScriptManager.RegisterStartupScript(Me.Page, Me.GetType, "SetFocus", "$get('" & TXT_EmpID.ClientID & "').focus();", True)
        Else
            LBL_Error.Text = ""
        End If
    End Sub
 
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        TXT_EmpID.Attributes("onblur") = "$get('" & BTN_Check.ClientID & "').click();"
    End Sub
End Class

Open in new window

0
 
LVL 6

Expert Comment

by:hehdaddy
ID: 24216413
Hi there. If you use a CustomValidator control on your page, it has a "SetFocusOnError" property that if set true will automatically focus the control if the validation fails.
0
 

Author Comment

by:preethamonline
ID: 24221737
Thanks guys for the suggestions. I am traveling now. Will check it on saturday and let u know.
0
 
LVL 1

Expert Comment

by:MarcGraff
ID: 24232061
Hey hehdaddy,

Yeah, you could do that with a customvalidator and a webservice. This would be move efficient but a little more complex.
0
 

Author Comment

by:preethamonline
ID: 24232502
Hi GUys, thanks you all for your comments. I am really missing the setfocus command that is used in access. Well, actually kind of a beginner to programming. Let me reexplain what i am intending to do. I shall also attach a screenshot, to simplify things.
Well, when the user enter an empid, when the focus goes to the next textbox, it should check in the database if this empid exists otr not.
What i was thinking was read the value from the database and then compare it with the txtempid field.
Now, i dont know how to validate this. either when the user click on the next field or the focus is on the next field. I hope i am not confusing you guys.

Below is the code used to read the data from the datanase

Dim con As New System.Data.OleDb.OleDbConnection
        Dim dreader As System.Data.OleDb.OleDbDataReader
        Dim myCommand As New System.Data.OleDb.OleDbCommand
        Dim myPath As String
 
        myPath = Server.MapPath("hayat.mdb")
        con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data source=" & myPath & ";"
        myCommand.CommandText = "select * from empdetails where empid = " & txtempid.Text & ""
        myCommand.Connection = con
        con.Open()
        dreader = myCommand.ExecuteReader
 
        While dreader.Read()
            txtempidcheck.Text = dreader(1)
            If (txtempidcheck.Text) = (txtempid.Text) Then
                lblmsg.Text = "Please enter a unique id"
                txtempid.setfocus
else
txtempname.setfocus
            End If
        End While
 
        dreader.Close()
        con.Close()
1.bmp
0
 
LVL 6

Assisted Solution

by:hehdaddy
hehdaddy earned 600 total points
ID: 24232562
Hi there,

I think the easiest and most unobtrusive way would be to use the ASP.NET AJAX update panel, and a OnTextChanged event with AutoPostBack="true" on the empid.

Go to the asp.net website for easy examples of how to use the update panel.

What happens is that when the text changes in the textbox and loses focus, it will trigger an AJAX partial postback on the empid control. Then you can do your database stuff and asp.net will return to the page without a full page postback.

Your textbox would look a little something like this. This is just an example:

<asp:TextBox ID="empid" runat="server" AutoPostBack="true" OnTextChanged="TextBox1_TextChanged"></asp:TextBox>

0
 
LVL 6

Expert Comment

by:hehdaddy
ID: 24232568
My previous example is a bit incorrect:
<asp:TextBox ID="empid" runat="server" AutoPostBack="true" OnTextChanged="empid_TextChanged"></asp:TextBox>
0
 

Author Comment

by:preethamonline
ID: 24232625
ok, me going to learn more about the update panel. will keep you guys updated. I am using visual studio.net 2003. do i have it in the toolbox
0
 

Author Comment

by:preethamonline
ID: 24232645
or should i just scratch the application and start doing it using visual web developer 2008
0
 
LVL 6

Expert Comment

by:hehdaddy
ID: 24232821
I would use Visual Web Developer 2008. It will make your life much better.
0
 

Author Comment

by:preethamonline
ID: 24232829
can we use the ASP.NET AJAX toolkit in visual studio.net 2003?
0
 

Author Comment

by:preethamonline
ID: 24232880
ok. i have just downloaded visual web developer 2008. will try working on it.
0
 

Author Comment

by:preethamonline
ID: 24232937
Hi hehdaddy, i know, it may sound stupid, but since this is the first time i'm using Visual web developer 2008, i was wondering as to how to create space between two controls.
For eg: When i drag a text box, and a label , it appears so close together. I'm not able to reposition the controls. Can u pls tell me as to how can i do it.
0
 

Author Comment

by:preethamonline
ID: 24232996
Ok kinda figured it out. Select the control, goto format, positioning and then select absoluter right?
0
 

Author Comment

by:preethamonline
ID: 24233197
Thanks very much guys. I'm likking Visual Web developer quite a lot. Its the first time i'm using. I would like to give the points to both of you.
If i have any doubts i would post another question.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses
Course of the Month18 days, 17 hours left to enroll

834 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