Download Enter Key in ASP.NET

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
'
cmd inserimento di una riga in crmtrattative
'cmd = New OleDbCommand("insert into crmtrattative
(stato,utente,cliente) values(?,?,?)", cn)
'cmd.Parameters.Add(New OleDbParameter("@stato",
OleDbType.Numeric)).Value = statoavanza.SelectedValue
'cmd.Parameters.Add(New OleDbParameter("@utente",
OleDbType.Numeric)).Value = Session("utente")
'cmd.Parameters.Add(New OleDbParameter("@cliente",
OleDbType.Numeric)).Value = sender.CommandArgument
'cmd.ExecuteNonQuery()
'cmd = New OleDbCommand("SELECT MAX(codice) from crmtrattative where
stato=? and utente=? and cliente=?", cn)
'cmd.Parameters.Add(New OleDbParameter("@stato",
OleDbType.Numeric)).Value = statoavanza.SelectedValue
'cmd.Parameters.Add(New OleDbParameter("@utente",
OleDbType.Numeric)).Value = Session("utente")
'cmd.Parameters.Add(New OleDbParameter("@cliente",
OleDbType.Numeric)).Value = sender.CommandArgument
'dr = cmd.ExecuteReader
'If dr.Read Then
'
Dim a As Integer = 0
'
a = dr(0) 'contine il codice della trattativa appena inserito
'
dr.Close()
'
dr = Nothing
'
cn.Close()
'
cn = Nothing
ASP.NET Tutorials
Enter Key in ASP.NET - Complete Research
One of the common requests in ASP.NET is
to submit a form when visitor hit an Enter key. That could be a case if, for example you
want to make Login Screen. It is expected that user just hit enter when he insert a
password instead to of forcing him to use a mouse to click login button. If you want to
make search function on your web site, it is frequently required to give a possiblity to hit
enter after you insert a search terms instead of mouse click on a Search button.
In classic HTML or ASP pages, it is not hard to submit forms using the enter key.
Programmer use a <input type="submit"> to make a default button. If web site visitor
click on that button or press enter key, the form will be submited.
Off course, you can have a more than one form on your page and individual submit
button for every form.
You don't want to submit a form with Enter key?
Rarely, you will need to disable an Enter key and avoid to submit form. If you want to
prevent it completely, you need to use OnKeyDown handler on <body> tag of your page.
The javascript code should be:
if (window.event.keyCode == 13)
{
event.returnValue=false;
event.cancel = true;
}
ASP.NET problems with Enter key
If you try to use Enter key in ASP.NET, according to your browser's type, you can get
really weird results. Try to place one ASP.NET textbox and a button to the web form.
Write a code on a OnClick event of a button. That could be something simple, like:
Response.Write("The button was clicked!");
Now start debuging and write something to textbox. If you press enter while focus is on
textbox, form will submit, but your code for button's click event will not be executed.
Stop the debuging and place one simple HTML textbox to the form. You will not write
anything in this textbox, so you can make it invisible. Just place this HTML code
somewhere inside of your form tag.
<INPUT type="text" style="VISIBILITY: hidden;POSITION: absolute">
Start debuging again. You cannot see the second textbox, and everything looks like
before. Try again to write something in visible textbox. If you press enter now, form will
submit, and also your code for button's click event will be executed. This is extremelly
different behavior, and you did nothing except you placed one inivisible HTML textbox on
web form. :)
Maybe it is not elegant, but placing invisible textbox could be simple solution for you if
you have only one button on your web form. But, what if you have a different situation?
What if you have a few buttons with only one textbox, more than one text box with only
one button, or many text boxes and many buttons on form?
Different browsers has a different behaviors in these cases. if you have more buttons,
only first button will be "clicked" every time. So, we need some other approach to get an
universal solution.
How to make a default button in ASP.NET
We need to specify exactly what button will be "cliked" when visitor press Enter key,
according to what textbox currently has a focus. The solution could be to add onkeydown
attribute to textbox control:
TextBox1.Attributes.Add("onkeydown", "if(event.which || event.keyCode){if
((event.which == 13) || (event.keyCode == 13))
{document.getElementById('"+Button1.UniqueID+"').click();return false;}}
else {return true}; ");
key and cursor is placed in TextBox1 textbox. On this way you can "connect" as many
text boxes and buttons as you want.