making enter key in server side textbox cause a server side button event


  I have a asp control textbox with an asp button next to it ( ie both server side asp controls).

  If  I press the enter key when focus is on the textbox - how can I trigger the enter button event to fire off at the server ?

  Many thanks?
9/23/2005 2:50:10 PM 91979 articles. 4 followers. Follow

7 Replies

Similar Articles

[PageSpeed] 45


Regarding this...

 ubique wrote:

...I have a asp control textbox with an asp button next to it ( ie both server side asp controls).

If  I press the enter key when focus is on the textbox - how can I trigger the enter button event to fire off at the server ?

...I have to ask: why do this?

...why not simply have the enduser press Tab and then press Enter, which is much more simple to implement and serves the same purpose?

I know-- "the customer wants it". However, I look at this sort of things as "a non-critical enhancement... in short, a waste of development time and energy, which is precious because there are, invariably, many other critical issues that need fixing. I usually push back by saying something like "sure, I can do that, but isn't fixing featureA, featureB, and featureC more important given that the application simply will not work without them? ... and, besides, we are already late and over-budget, so is that textbox thing really critical to fix right now?".

A reasonable customer will say "you are right and we can wait on that functionality" and then move on. An unreasonable customer, on the other hand, needs to be (in one way or another) kept from undermining the success of the project due to his/her faulty approach-- in the end, when the project is delivered, he/she will see the truth of the matter, (assuming, of course, that the shared goal of all involved is to actually get the project done-- if that shared goal does not exist, then it is not really a project but a "strange state of affairs").

Sure, if there are zero bugs to fix, if the application is optimized, if the application is otherwise feature complete, if the code is well documented, if the design is well documented, if you are ahead of schedule, and in there is money in the budget, then do it. Otherwise, focus on the real work.

I try to ask myself these questions--

--If I finished the project tomorrow, and feature/change/fix (A) were not present, would the application work properly?


--Given that case, would the customer be more-or-less happy that the current death-march is finally over?

If the answers are "yes" and "yes", then I simply put (A) on the list of "things to do after the important stuff is done" and move onward.


Thank you.
9/23/2005 6:04:35 PM

Try this:

// Add to your Page_Load event handler:

StringBuilder sbScript = new StringBuilder();

sbScript.Append("<script language=\"JavaScript\" type=\"text/javascript\">\n");
sbScript.Append("function documentOnKeyPress()\n");
sbScript.Append(" var charCode = window.event.keyCode;\n");
sbScript.Append(" var elementType = window.event.srcElement.type;\n");
sbScript.Append(" var defaultButtonId = window.event.srcElement.defaultButton;\n");
sbScript.Append(" if ( (charCode == 13) && (elementType == 'text') )\n");
sbScript.Append(" {\n");
sbScript.Append(" // Cancel the keystroke completely\n");
sbScript.Append(" window.event.returnValue = false;\n");
sbScript.Append(" window.event.cancel = true;\n");
sbScript.Append(" if ( document.getElementById(defaultButtonId).click )\n");
sbScript.Append(" document.getElementById(defaultButtonId).click();\n");
sbScript.Append(" }\n");
sbScript.Append("document.onkeypress = documentOnKeyPress;\n");

this.RegisterClientScriptBlock("OnKeyPressScript",  sbScript.ToString());

// Make ButtonA the default button for TextBoxA and ButtonB the default button for TextBoxB, etc...
this.TextBox1.Attributes.Add("defaultButton", this.ButtonA.ClientID);
this.TextBoxB.Attributes.Add("defaultButton", this.ButtonB.ClientID);


9/23/2005 6:35:05 PM
valid point mkamoski, but most of the time we are doing the work for the client. To a client, user-friendliness (which is offered by pairing the textbox with an enter key for e.g), may worth more than making the app faster than a speed of light. 

More often that not, a  user wouldnt mind waiting for the application to process (well they can make a cup of tea while waiting). But that milisecond wasted to move his/her hand from the keyboard to a mouse is a precious milisecond wasted!

In terms of this perspective, pairing the enter key with the textbox might be also considered as a system performance increase.

Anyway, well done for the code. It works for me as well!
10/28/2005 9:56:21 AM
Sorry, I have to agree with Mkamoski on this one as what happens if there are more controls on the page other than the TextBox and Button, or you come back and need to add some later? All you're going to do is have problems then.

Since when is submission the standard when pressing the enter key while in a TextBox? Never heard of that in any other kind of application. I would think that intuitively, the user would expect a new line rather than submission. I know that I sure would!

10/28/2005 11:14:51 AM
> I would think that intuitively, the user would expect a new line rather than submission. I know that I sure would!

You're totally right NC01. Just curious would you normally use google. Type..then click search? or type keyword and press enter? (this is not sarcastic, I really do curious on what ppl do and expect in terms of GUI)
11/17/2005 12:49:42 AM
I have never (and would never) hit the enter key on a single line TextBox except by accident!

11/17/2005 2:19:23 AM

Submit on enter is a part of HTML spec. It is also how Unix variants (including Linux) and some Windows applications work. It is just how it is. If .Net did something different, it would break the Internet, and Microsoft has already done many a time...


In HTML, the enter key (or return key as it is often called) to is designed to "click" the first submit button in the form. That is what it is supposed to do. Tab is the HTML command to switch fields.. That's how it has been since the invention of HTML forms.

11/17/2005 2:47:33 AM

Similar Artilces:

Firing both Client side event and server side event for server side button
I am having some difficulties getting this to work.. I have button_click ( and ClientClick (Javascript).  Javascript is firing fine.  Server script is not. may be you have return return false in the in the javascript which is why its not firing server side event.You should add javascript evelt like thisbutton1.onclientclick = "functionname" Function should nopt return false. Vikram www.vikramlakhotia.comPlease mark the answer if it helped you...

how can I update server side control purely by events triggered by server side timer
Currently I am writing code based on google earth API. Basically server writes a piece of KML info every second,(using AddHandler aTimer.Elapsed, AddressOf OnTimer) then the browser uses XMLHttpRequest to retrieve this KML and display it on the Google Earth plugin. This part works fine, however when there is no more KML to write on the server, I want to disable some buttons running on the server. Unfortunately I cannot achieve it. If I use update method of UpdatePanel (I put those buttons in the UpdatePanel), it pops up the error of "The Update method is called during or after the page...

How to access Client Side control in server side button click event?
Hi all, I need to access client side control from server side code. For an example <form id="form1" runat="server"> <table> <tr> <td>UserName:</td> <td><input type="text" id="textBoxUserName" /></td> <td><asp:Button ID="buttonShow" runat="server" Text="show" OnClick="buttonShow_Click" /></td> </tr> </table>   </form> I need to access the textBoxUsername in the server side button click event. How to do that? Than...

I need to get a server side button to run this client side code?
I need to get a server side button to run this client side code when the server button is clicked and then subsequently called by the server. Is it possible? The problem is that the print control is a client side activex. I can call it normally using a client side button but this is no good when I need to print more then one page. Any help would be great. Thanks. The code: function PrintProc(){ var MyPrinter; MyPrinter = new ActiveXObject("ImgXPrint6.ImgXPrint"); MyPrinter.PageFrom = 1; MyPrinter.PageTo = 1; MyPrinter.PageM...

what client-side and server-side events are fired when button X is hit?
Folks,In a web aspx page, I use a client-side (javascript) button named "Cancel" so when users hit it, if the data they enter is not saved yet, then this client-side button prompts users to save data before the page is closed. I code this client-side button with event "onclick" and thing like <input type=button ... onclick="btnCancelClick();" >   My question is if users hit the right top corner button X (next to the maximize and minimize buttons also on the top right corner of the aspx web window page), then what client-side (javascript) and serv...

Server side code not being parsed on server side
I have a frameset designed to display data for a customer from multiple pages.  the server side script that puts the customer id into the Query String isn't being parsed. The customer's id is in the page's url as ?id=5  The code:<frame src='../Basic/Default.aspx?id=<% Response.Write(Request.QueryString("id")) %>' name="Basic" scrolling="No" noresize="noresize" id="topFrame" title="topFrame" />  ruturns this: Server Error in '/Server' Application. Input string was not i...

TextBox server side event has to Fire when Enter Some Text
Hai     Here TextBox server side  event has to Fire when Enter Some Text in the sense OnTextChanged Event has to fire when enter text or an empty space in Textbox. See my code  <asp:TextBox ID="txtSearch" runat="server" OnTextChanged="OnTextChanged_Changed" AutoPostBack="true"></asp:TextBox> code(.CS)protected void OnTextChanged_Changed(object sender, EventArgs e) { ---- Code }    Any Help   Thanks in Advance.....   Hi, TextChanged event will fire whenever some text is ...

Help with client side events Vs. server side events
Hi, Can someone look as the following script and tell me why marking the check box doesn't generate a client side event eventhough there's: onclick="checkClick()"? marked.  Thanks in advance, Sivan <%@ Page Language="c#" %><HTML> <HEAD>  <meta name="vs_targetSchema" content=""> </HEAD> <body>  <SCRIPT language="c#" runat="Server">  void Page_Load(object s, EventArgs e) {  if (!Page.IsPostBack)  {   Label1.Text = Label...

hit Enter in TextBox to always trigger a server-side event
I've reviewed quite a few posts to this forum related to the following topic and I don't believe that a production-level solution has been described. For web and non-web UIs alike : when the cursor is located in certain text-boxes in the app's window, many users expect (desire) that keystroking "Enter" will trigger submission of their current entries in the controls : Such submission might cause data-processing steps to be executed , or reconfiguration of the interface , or both ... In ASP.NET , this translates to : Keystroking Enter when a particular TextBox on the pag...

How To Cause a Button's Click Event to Fire from Server Side
I need to cause a Buttons Event to Fire should a criteria Not be met, How can I do this in Server code? Thanks in Advance.Net Web/Software Engineer You can't actually cause the event to fire, but you can certainly call the handler just as you would any other method.Thanks, EdMicrosoft MVP - ASP/ASP.NET   I am a newbie when it comes to Events and Delegates. What I want to do with this cause this event to fire whenever a certain a criteria is met. How do I go about doing this ??? thanks.Net Web/Software Engineer Like I said, you can simply call the event handler like this: ...

server side controls in a non-server side form
I want to use server side controls in a non-server side form.I use server side controls (like dropdownlists) so I can fill them with the right options from the database.My question is, what is the best way to approach this?CAN I use server-side controls in a client side form? If so, how can I get the values contained in the controls when I do a post? (not in a querystring).Here's a sample of my code: <form id="frmAdvancedSearch" method="post" action="advancedSearch.aspx"> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <input type="submit" /> </form>...

pls help: how to get client-side HTML "text field" value from a server side button
i copy some javascript from somewhere so user can choose pick up a date from a calendar and display the date onto a HTML text field. I have no knowledge in writing client side script(i.e. write code in html) can i write some code in a server-side button to get the value in the HTML text field??? i tried: Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click textbox1.text = document.getElementById("htmltext").value End Sub but "document" is highlighted, and say isn't declar yet. and...

How to create a popup from server side after a server side validation
Hi, I am able to popup a window frolm server by adding the following in Page_Load(). imageButton.Attributes["onclick"] = "javascript:return "+"confirm('Are you sure you want to modify ?');"; But now i want to popup conditionally i.e. after a server side validation. If the server side validation fails, only then show a popup. Thanks, asmith. You've got the order of things wrong. The imagebutton's onclick event will fire first, as soon as the button is pressed. The page hasn't submitted yet. Once the page submits, the server side code run...

Start Client Timer from Server Event and start server Event from client event
All of you, I have a big challenge which i'm not able to resolve. I need some experts with samples on this. Situation for a webbased quiz system I have a data set with e.g. 5 questions. The process should be like 1. Load Question 1 from database, there is also a picture and a MP3 sound file 2. I connect the MP3 sound file to a webcontrol I've written to play Mp3 with a flashmovie. 3. Once this mp3 is played (the question is read for people with reading problems) an Event should be raised from the client to the server. 4. The event from 3 should start a Countdown timer (15 se...

Web resources about - making enter key in server side textbox cause a server side button event -

Aqua (user interface) - Wikipedia, the free encyclopedia
Aqua is the GUI and primary visual theme of Apple Inc. 's OS X operating system . It is based around the theme of water, as its name suggests, ...

Wikipedia:Sockpuppet investigations - Wikipedia, the free encyclopedia
WP:RFCU redirects here. You may be looking for Wikipedia:Requests for comment/User conduct ( WP:RFC/U ), CheckUser policy ( Wikipedia:CheckUser ...

Help - is a social service . Users can post short(140 character) status messages which are broadcast totheir friends and colleagues on the ...

City of Whitehorse, YT : Contact
Employment - Bylaws - Document Library - FAQ - Contact Us Search Query Textbox Home Home About Whitehorse Notify Me Contact Us Employee Links ... Help
Help for uploading pictures Noun explanation Group pictures: a basic unit which stands for a piece of news photo in system; include: information ...

Metamorphose: All-In-One File/Folder Renamer For Windows, Mac & Linux
Previously, we’ve reviewed many batch file-renaming applications like ReNamer (read review), NameChanger (read review), File Grinder (read review) ...

How to build a Twitter extension for Chrome
Ars takes a close look at the Chrome Web browser's new extension system. In …

ASUSTeK Computer Inc. -Support- RSS
... RSS Service, please follow the instructions below. STEP 1. Select the product to subscribe. STEP 2. The RSS link will be shown on the Textbox ...

authonomy writers and readers - Daniel Escurel Occeno's profile - authonomy writing community
... of the Point Guard”. It is entered in writing contest.In the TEXTNOVEL website simply type in “Lee Chon” in the textbox search ...

Baseball Prospectus
Forgot Your Password? Enter your username in the textbox below. Username: If you don't remember your username, please contact Customer Service ...

Resources last updated: 12/29/2015 4:57:56 PM