Active Server Pages Programming (ASP)/Pressing of Enter Key
Expert: Srini Nagarajan - 6/5/2005
QuestionThere are 20 textboxes on my web form (of ASP.NET)
(made for only data entry) and high
speed data entry operator will use it.
They will just feed the values in textboxes
and will press enter key for next
field. I want, when user press enter key in any textbox, control should be focused in next text box and if it is last text box in form, it should go to save button.
After filling all entry the
data will be save into table of SQL
Server.
What steps shoul be taken in
this regard, please guide me.
Thanks in advance.
Warm Regards,
Girish
from :
gksharma481@hotmail.com
AnswerHi
I use this change the tab key value "9" to enter key "13"
Here is the simple code
/// <summary>
/// This causes the enter key to behave like the tab key for an input control.
/// </summary>
/// <param name="TextBoxToModify">
/// This is the textbox to have modified enter key behavior. It doesn't have to be a TextBox control, but must be derived from either HtmlControl or WebControl,
/// and the html control should accept an 'onkeydown' attribute.
/// </param>
public static void TabOnEnterKey( Control TextBoxToModify )
{
// This is our javascript - we fire the client-side click event of the button if the enter key is pressed.
string jsString = "if ((event.which && event.which == 13) || (event.keyCode && event.keyCode == 13)) {event.keyCode=9;} ";
// We attach this to the onkeydown attribute - we have to cater for HtmlControl or WebControl.
if (TextBoxToModify is System.Web.UI.HtmlControls.HtmlControl)
((System.Web.UI.HtmlControls.HtmlControl)TextBoxToModify).Attributes.Add("onkeydown",jsString);
else if (TextBoxToModify is System.Web.UI.WebControls.WebControl)
((System.Web.UI.WebControls.WebControl)TextBoxToModify).Attributes.Add("onkeydown",jsString);
else
{
// We throw an exception if TextBoxToTie is not of type HtmlControl or WebControl.
throw new ArgumentException("Control TextBoxToModify should be derived from either System.Web.UI.HtmlControls.HtmlControl or System.Web.UI.WebControls.WebControl", "TextBoxToModify");
}
}
OR
You can do it using Simple Javascript Here is the code for it.
<!-- ONE STEP TO INSTALL ENTER KEY FOCUS:
1. Copy the coding into the BODY of your HTML document -->
<!-- STEP ONE: Paste this code into the BODY of your HTML document -->
<BODY>
<!-- This script and many more are available free online at -->
<FORM METHOD="POST">
Name: <INPUT TYPE="TEXT" onKeyDown="if(event.keyCode==13) event.keyCode=9;"><BR>
Address: <INPUT TYPE="TEXT" onKeyDown="if(event.keyCode==13) event.keyCode=9;"><BR>
City: <INPUT TYPE="TEXT" onKeyDown="if(event.keyCode==13) event.keyCode=9;"><BR>
<INPUT TYPE="submit" Value="Ok">
<INPUT TYPE="reset" Value="Cancel">
</FORM>
for saving data into database
Adding records to a SQL Server 2000 database using ADO.NET a fairly strait forward process, much like adding records to an Access Database.
First, you will need to import the System.Data.SqlClient name space at the top of your page.
Imports System.Data.SqlClient
Next, we write the actual code to connect to SQL Server and insert the data. We apply the below commands to the Click Event of a button control. Notice that we are getting our values from the .Text property of the txtFirstName TextBox and the txtLastName TextBox.
Dim cmd As New SqlCommand("INSERT INTO Customers (FirstName, LastName)VALUES('" & txtFirstName.Text & "','" & txtLastName.Text & "')", New SqlConnection(strConn))
cmd.Connection.Open()
cmd.ExecuteNonQuery()
cmd.Connection.Close()
After we add the data above, we want to display it on the page along with all past records added. This is done by calling a Method that will bind the data to a repeater control.
Dim cmd As New SqlCommand("Select * From Customers", New SqlConnection(strConn))
cmd.Connection.Open()
Repeater1.DataSource = cmd.ExecuteReader
Repeater1.DataBind()
cmd.Connection.Close()
cmd.Connection.Dispose()
The full listing for the code behind page is as follows
Imports System.Data.SqlClient
Public Class index
Inherits System.Web.UI.Page
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub
Protected WithEvents btnInsert As System.Web.UI.WebControls.Button
Protected WithEvents txtFirstName As System.Web.UI.WebControls.TextBox
Protected WithEvents txtLastName As System.Web.UI.WebControls.TextBox
Protected WithEvents Repeater1 As System.Web.UI.WebControls.Repeater
'NOTE: The following placeholder declaration is required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
Public strConn As String = "data source=xx.xx.xxx.xxx; User ID=xxxxxx; Password=xxxxxx; Persist Security Info=True;packet size=4096"
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
LoadData()
End Sub
Private Sub btnInsert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInsert.Click
Dim strConn As String = "data source=xx.xx.xxx.xxx; User ID=xxxxxx; Password=xxxxxx; Persist Security Info=True;packet size=4096"
Dim cmd As New SqlCommand("INSERT INTO Customers (FirstName, LastName)VALUES('" & txtFirstName.Text & "','" & txtLastName.Text & "')", New SqlConnection(strConn))
cmd.Connection.Open()
cmd.ExecuteNonQuery()
cmd.Connection.Close()
LoadData()
End Sub
Sub LoadData()
Dim strConn As String = "data source=xx.xx.xxx.xxx; User ID=xxxxxx; Password=xxxxxx; Persist Security Info=True;packet size=4096"
Dim strSQL As String = "Select * From Customers"
Dim cmd As New SqlCommand(strSQL, New SqlConnection(strConn))
cmd.Connection.Open()
Repeater1.DataSource = cmd.ExecuteReader
Repeater1.DataBind()
cmd.Connection.Close()
cmd.Connection.Dispose()
End Sub
End Class
Happy Programming!
-Srini