Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Run button click event after enter is pressed

Status
Not open for further replies.

Mike555

Technical User
Joined
Feb 21, 2003
Messages
1,200
Location
US
I have a webform containing a textbox control and a button control. The user populates the textbox and then clicks the button, which subsequently runs a click_event. How could I setup the textbox so that the user can simply press the keyboard "Enter" button to fire the button's click_event instead of having to click the button?

--
Mike
 
You'd have to do it with client scripting -

1) Catch keys pressed when focus is on the textbox.
2) Run the __doPostBack (I think it's called ;)) client-side method when you catch the enter key being pressed.

-----------------------------------------------
"The night sky over the planet Krikkit is the least interesting sight in the entire universe."
-Hitch Hiker's Guide To The Galaxy
 
AtomicChip, Thank you for your response. I was hoping it would be a bit easier :)

--
Mike
 
Actually, I forgot to mention (sorry, wasn't totally awake when I posted that) -

Andy Smith had created a default button control that may be the solution you're looking for. I use it in pretty much all my personal projects (well, before I converted my server to a *nix box). You can find it here -
-----------------------------------------------
"The night sky over the planet Krikkit is the least interesting sight in the entire universe."
-Hitch Hiker's Guide To The Galaxy
 
tgreer -

I don't believe that will work - .net handles form posting differently than traditional scripted pages. As far as I know, you cannot just call form.submit() and allow any processing to be done. When the page is reloaded on postback, the form will not know what to do.

-----------------------------------------------
"The night sky over the planet Krikkit is the least interesting sight in the entire universe."
-Hitch Hiker's Guide To The Galaxy
 
Right.

However, I noticed that your homepage is written in PHP (I'm assuming that's your most-used language?) - form.submit() works great for scripted language such as PHP, as there's a value to the action parameter of the form, telling the client where to go with the form.submit(). On the other hand, with .net forms, there isn't (and remember - there's only one runat=server form allowed on a .net page).

When I need to force the client to do a postback (and I'm not using Andy's default button control), I'll add a server button to the page, add whatever functionality I need to it's Click event, and then I'll call Page.GetPostBackEventReference(btnName), which will cause the proper __doPostBack client script method to be rendered to the user's browser and will cause my button's script to be executed (event if the button is not visible).

...Or is there something here I don't know?...

-----------------------------------------------
"The night sky over the planet Krikkit is the least interesting sight in the entire universe."
-Hitch Hiker's Guide To The Galaxy
 
I do most of my coding in C#/ASP.NET. My website is hosted on an Apache server, though. I don't do any ASP.NET there. The only thing the PHP is used for is to conditionally load stylesheets (see the Articles page).

I appreciate this discussion, and I'm willing to admit there is something *I don't know*. But I've never had a problem with ASP.NET sites to which I've added JavaScript .submit() code.

If you closely examine the HTML rendered by ASP.NET, you'll see it is doing a submit(), only it's doing it within it's own JavaScript function "_doPostBack()" Now, if I preempt that and force the page to submit, might I lose some session state/viewstate parameters? Maybe. If so, it hasn't hurt me. That's not to say it won't. I see it this way: if I wanted such information as, which control/event happened, I'll code that to the control, and let it fire the proper postback/submission.


In cases where I want to "manually" submit the form, I'm usually setting my own variables, which I'll interrogate in the Page_Load method. In other words, I don't need what might be missing, in that particular case.

You've given me some homework to do! In any case, the user could just as well call the _doPostBack method in lieu of a direct .submit().




Thomas D. Greer

Providing PostScript & PDF
Training, Development & Consulting
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top