473,403 Members | 2,354 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

About Enter key in vb.net code behind

3
Hi,
I'm using vb.net 2005. In my code behind i wrote like this code..
Expand|Select|Wrap|Line Numbers
  1. Partial Class SearchTxt
  2.     Inherits System.Web.UI.Page
  3.     Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  4.  
  5.     End Sub
  6.     Protected Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
  7.         If Not TextBox1.Text = "" Then
  8.             Response.Redirect("Page2.aspx?txt=" & TextBox1.Text)
  9.         End If
  10.     End Sub
  11. End Class
  12.  
Now what i want is i want the page to redirect only if the enter key is pressed inside the textbox...Please tell me how to write the code inside TextBox1_TextChanged..
Dec 18 '08 #1
7 6505
Frinavale
9,735 Expert Mod 8TB
You can only check which key is pressed in client side code.
So you're going to have to write JavaScript that checks to see if the enter key is pressed when the user is in the text box. You're going to have to write JavaScript functions that handle the OnBlur, and OnChange events to do this:

OnChange event: you should check to see which key the user's entered...if the user's hit the enter key, store a "True" value in a hidden field that you can then retrieve in the server code so that you can determine what to do.

OnBlur event: store a "False" value in the hidden field in order to indicate that you are no longer in the text box.

-Frinny
Dec 18 '08 #2
Plater
7,872 Expert 4TB
Pressing enter on an input field just fires a postback. None of your specific button events should fire (unless there is a default button assigned to the form)
Dec 18 '08 #3
Frinavale
9,735 Expert Mod 8TB
@Plater
If you set the function that handles OnChange event to return False... and in that function catch the case when the enter key is hit I think it should prevent it from firing....regardless I think the OnChange event happens before the enter key gets to submit the forum.

I'm going to try it now.
Dec 18 '08 #4
Frinavale
9,735 Expert Mod 8TB
The following JavaScript checks if the user pressed Enter:
Expand|Select|Wrap|Line Numbers
  1.  function checkEnter(e)
  2.        {
  3.          var unicode;
  4.          try
  5.          {   /*IE*/
  6.              unicode = e.keyCode; 
  7.          }
  8.         catch(err)
  9.          {   
  10.              try
  11.              { /*Netscape, Mozilla, FireFox...*/
  12.                   unicode = window.event.keyCode;
  13.  
  14.              }
  15.              catch(error)
  16.              {  /*Other*/
  17.                  unicode = e.which;
  18.               }
  19.           }
  20.           //displays an alert  and returns false
  21.          //if enter is pressed
  22.          if(unicode == 13)
  23.          { alert("enter pressed");
  24.            return false;
  25.          } 
  26.          return true;
  27.        }
In my ASP code I created a TextBox
Expand|Select|Wrap|Line Numbers
  1.   <asp:TextBox ID="testTextBox" runat="server"></asp:TextBox>
And in my PageLoad event I set the TextBox to execute the function every time a key's pressed:

Expand|Select|Wrap|Line Numbers
  1. Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  2.         If IsPostBack = False Then
  3.             testTextBox.Attributes.Add("onkeypress", "return checkEnter(event);")
  4.         End If
  5. End Sub
  6.  
Note that I set the "onkeypress" event to return the function call. The enter action is cancelled because I'm returning false here...

So this suggests that the JavaScript is executed before the page is sent to the server....
-Frinny
Dec 18 '08 #5
Plater
7,872 Expert 4TB
isn't window.event just IE?
Javascript - Event accessing
Dec 18 '08 #6
Frinavale
9,735 Expert Mod 8TB
@Plater
Hmmm....
Apparently not because I tested that in FireFox.
Dec 18 '08 #7
Plater
7,872 Expert 4TB
Hmm everything I read about event handling says mozilla based uses the "e" but in IE you could use window.event

Expand|Select|Wrap|Line Numbers
  1. function OnlyNumbers(e)
  2. {
  3.     var keynum;   
  4.     if(window.event) // IE
  5.     {
  6.         keynum = e.keyCode;
  7.     }
  8.     else if(e.which) // Netscape/Firefox/Opera
  9.     {
  10.         keynum = e.which;   
  11.     }
  12. //...
  13. }
  14.  
Although I am sure they have unified somewhat in their behavior
Dec 19 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

125
by: Sarah Tanembaum | last post by:
Beside its an opensource and supported by community, what's the fundamental differences between PostgreSQL and those high-price commercial database (and some are bloated such as Oracle) from...
5
by: Steve | last post by:
Hi, Is it possible to make hitting the enter key in an ASP textbox run the code behind an ASP button on a form? I have a search page which users tend to type in the query then just hit enter...
11
by: Sandra Castellanos | last post by:
Hello, I want to know what I have to do to make the enter key to submit forms. I read in another newsgroup that inserting an html hidden text box made the trick, and it actually does, but in my...
4
by: Paul W | last post by:
On my simple login-screen I have a 'username' and 'password' field and an imagebutton for the 'OK'. This used to work nicely so that when the user hit <Enter> from the password field, it invoked...
1
by: mdipiet | last post by:
I've got a form that is supposed to validate data entry from a bar code scanner. The scanner is set up to add a carriage return at the end of the data in the barcode, which should fire the...
8
by: Joe | last post by:
Hi, I have a form with three text fields and a Submit button. The two text fields have ReqiredFieldValidator and third text field has RegularExpressionValidator. The page validation works fine...
0
by: Tom Edelbrok | last post by:
I'm using VS 2005 to develop an intranet asp.net web application and I get a weird situation. If I start out with any ASPX page that contains an ImageButton control followed by a TextBox control,...
2
by: Seguros Catatumbo | last post by:
Hi, i have decent experience with asp 3.0 (classic), and downloaded web developer express to see what's the fuzz over it. I have lots of questions, mostly regarding the use of forms and database...
4
by: nkoier | last post by:
Hi, I've been going crazy trying to figure out what's wrong with our Asp.Net 2.0 intranet site. At the very top of our main page I provide a TextBox and a Button for submitting Google searches....
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.