473,385 Members | 1,919 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,385 software developers and data experts.

Figuring out which form button was pressed

On my vb.net page, I have 4 sets of inputs + form buttons.

example:

Search: [___________] (GO)
Zip: [____________] (GO)
County: [select a county | \/ ] (GO)
County: [select a building | \/ ] (GO)

The problem is if I go to the page, type in a zip code, and hit enter.

What happens is that the SEARCH button is triggered...not the ZIP button.

Is there any way to automatically move focus to the correct submit button
upon hitting return?

-Darrel

Jan 27 '06 #1
19 2147
darrel wrote:
On my vb.net page, I have 4 sets of inputs + form buttons.

example:

Search: [___________] (GO)
Zip: [____________] (GO)
County: [select a county | \/ ] (GO)
County: [select a building | \/ ] (GO)

The problem is if I go to the page, type in a zip code, and hit enter.

What happens is that the SEARCH button is triggered...not the ZIP button.

Is there any way to automatically move focus to the correct submit button
upon hitting return?


I'm sure this could be accomplished some how using JavaScript, but that
sounds like a bad design and will ultimately confuse many users who will
also not know what to expect when they hit the enter button. Why have
many "GO" buttons?

I'd seriously recommend rethinking your design unless you have a very
good reason to do that.

--
Sean
Jan 27 '06 #2
you could place a hidden field on each form with the same name e.g "hfField"
but give them all different values e.g "value ="Search", value="Zip".

The on hte server just say something like :

If request("hffield") = "Zip" then
do the zip code
elseIf request("hffield") = "Search" then
do the search code e.t.c.

Its a bit convoluted but it does work.

Hope this helps.

Jan 27 '06 #3
VB.Net can have one default button...

Add this to your page_load.

Page.RegisterHiddenField("__EVENTTARGET", "btnSearch")

If you're looking to have the appropriate button triggered based on
what field they're in, try javascript... but I would also rethink your
design.
Fao, Sean wrote:
darrel wrote:
On my vb.net page, I have 4 sets of inputs + form buttons.

example:

Search: [___________] (GO)
Zip: [____________] (GO)
County: [select a county | \/ ] (GO)
County: [select a building | \/ ] (GO)

The problem is if I go to the page, type in a zip code, and hit enter.

What happens is that the SEARCH button is triggered...not the ZIP button.

Is there any way to automatically move focus to the correct submit button
upon hitting return?


I'm sure this could be accomplished some how using JavaScript, but that
sounds like a bad design and will ultimately confuse many users who will
also not know what to expect when they hit the enter button. Why have
many "GO" buttons?

I'd seriously recommend rethinking your design unless you have a very
good reason to do that.

--
Sean


Jan 27 '06 #4
poppy wrote:
you could place a hidden field on each form with the same name e.g "hfField"
but give them all different values e.g "value ="Search", value="Zip".

The on hte server just say something like :

If request("hffield") = "Zip" then
do the zip code
elseIf request("hffield") = "Search" then
do the search code e.t.c.

Its a bit convoluted but it does work.


That won't work because the browser is automatically firing the Click
event for the Search button when he hits enter. Besides, I don't even
see a reason to use hidden fields, to begin with. Each individual
button can have an event handler in the server side code.

--
Sean
Jan 27 '06 #5
Place this javascript in your page:

function fnTrapKP(btnName){
if (event.keyCode == 13)
{
var btn = document.getElementById(btnName);
event.returnValue=false;
event.cancelBubble = true;
btn.click();
}
}

....then modify the attributes of each search field to pass in the
corresponding search button:

this.txtFindValue.Attributes.Add("onKeyPress", "fnTrapKP('" +
btnFind.ClientID +"');");
"darrel" <no*****@nowhere.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
On my vb.net page, I have 4 sets of inputs + form buttons.

example:

Search: [___________] (GO)
Zip: [____________] (GO)
County: [select a county | \/ ] (GO)
County: [select a building | \/ ] (GO)

The problem is if I go to the page, type in a zip code, and hit enter.

What happens is that the SEARCH button is triggered...not the ZIP button.

Is there any way to automatically move focus to the correct submit button
upon hitting return?

-Darrel

Jan 27 '06 #6
Mark Newmister wrote:
Place this javascript in your page:

function fnTrapKP(btnName){
if (event.keyCode == 13)
{
var btn = document.getElementById(btnName);
event.returnValue=false;
event.cancelBubble = true;
btn.click();
}
}

...then modify the attributes of each search field to pass in the
corresponding search button:

this.txtFindValue.Attributes.Add("onKeyPress", "fnTrapKP('" +
btnFind.ClientID +"');");


This will work, but I find it likely that confusion will set in when a
user enters text in two or more fields.

Off the top of my head, I can't remember everything that triggers an
onKeyPress event, however, consider the following example:

1) The user enters text in the Zip field
2) The user enters text in the Search field
3) The user sets focus back on the Zip field
4) The user hits the enter button

So which OnClick event is fired? I believe it's the button for the Zip
field (but the Enter key might have also fired the OnKeyPress event),
but the bottom line is that it's an obvious confusion. And the user
need not even set focus back to the Zip field. If two or more input
boxes contain data, it won't be readily obvious what's going to happen.

For this reason, I still recommend a design change unless there's a very
good reason to use this method.

--
Sean
Jan 27 '06 #7
> I'm sure this could be accomplished some how using JavaScript, but that
sounds like a bad design and will ultimately confuse many users who will
also not know what to expect when they hit the enter button. Why have
many "GO" buttons?


Becasue these are all independent concepts. They each do a different thing.

Ie, the search field has nothing to do with selecting a county.

-Darrel
Jan 27 '06 #8
> That won't work because the browser is automatically firing the Click
event for the Search button when he hits enter. Besides, I don't even see
a reason to use hidden fields, to begin with. Each individual button can
have an event handler in the server side code.


Right. The problem is really on the front end. By default, hitting return
triggers the first form submit button.

-Darrel
Jan 27 '06 #9
> If you're looking to have the appropriate button triggered based on
what field they're in, try javascript... but I would also rethink your
design.


I'm thinking that there just isn't a solution to this ;o)

The issue is that I need some sort of event in javascript to trigger focus
on the approporiate button. However, there isn't an event to trigger from.

-Darrel
Jan 27 '06 #10
> function fnTrapKP(btnName){
if (event.keyCode == 13)
{
var btn = document.getElementById(btnName);
event.returnValue=false;
event.cancelBubble = true;
btn.click();
}
}

...then modify the attributes of each search field to pass in the
corresponding search button:

this.txtFindValue.Attributes.Add("onKeyPress", "fnTrapKP('" +
btnFind.ClientID +"');");


This looks interesting. You explain what is happening? It appears that when
the search field is being editing, the onKeyPress trigger calls the
function. I'm not quite sure what the function is doing, though. It it
posting the form for me?

-Darrel
Jan 27 '06 #11
1) The user enters text in the Zip field
2) The user enters text in the Search field
3) The user sets focus back on the Zip field
4) The user hits the enter button

So which OnClick event is fired?
Valid concern, however these fields all do different things. So it'd be most
likely thata person would just enter one field. However, if they do, I think
it'd trigger the last field editied, correct? I'd find that an acceptable
behavior.
For this reason, I still recommend a design change unless there's a very
good reason to use this method.


I'd be up for that. Any suggestions on what you'd do to redesign it?

-Darrel
Jan 27 '06 #12
OK, I think I've figured out a way to handle this server side.

I'll have this:

Search: [___________] (GO)
Zip: [____________] (GO)
County: [select a county | \/ ] (GO)
Building: [select a building | \/ ] (GO)

If the ZIP, County, or Building buttons are clicked, I'll just execute the
page based on that button's event handler.

If the SEARCH button is pressed, I'll do a few things:

- if search field contains text then...perform search
- if not, if zip field contains text then...go to zip tool
- if not, if County is not set to default...go to county tool
etc...

Basically, I will just not assume a SEARCH button click was actually
intended to be a search and just check for items within it's own event
handler.

The Catch:

The catch is that the above 4 form fiels aren't be loaded by the same
usercontrol. Specifically, the SEARCH control is independant of the others,
even though they render on the same page. As such, I'm not quite sure how to
handle this...or if I even can when they all come from different controls.

-Darrel
Jan 27 '06 #13
darrel wrote:
I'm sure this could be accomplished some how using JavaScript, but that
sounds like a bad design and will ultimately confuse many users who will
also not know what to expect when they hit the enter button. Why have
many "GO" buttons?


Becasue these are all independent concepts. They each do a different thing.

Ie, the search field has nothing to do with selecting a county.


I wonder if I'm not understanding the ultimate goal. Usually if things
are unrelated, I separate them into separate pages, altogether. But
since you've put them all on one, I must assume that you did so for a
valid reason.

On that note, a possible alternative would be to have a single submit
button and form validation that prohibits two or more text boxes from
having input supplied in them. However, this would likely lead to user
frustration if the user hits the back button after performing one
function and moving on to another function, because the text from the
previous run would still be in the input box.

Is it possible/acceptable to perform two or more operations
simultaneously? If so, you could check to see which input boxes have
input and perform the corresponding operation for each of them.

Hope that helps,

--
Sean
Jan 27 '06 #14
I agree with Sean that you probably really don't need 4 buttons - just 1.
When the user clicks the one button just do a search using any field that is
populated - similar to the address search on MapQuest:

http://www.mapquest.com/

However, I would still use the code I provided to "click" the one button
when the user presses [Enter] so they don't necessarily have to reach for
the mouse and explicitly click it - i.e., convenience for the user.
It appears that when the search field is being editing, the onKeyPress
trigger calls the function.
Yes, when the search field has *focus* and you press [Enter], it will click
the button (object name) that you pass into the function. The object name
of your button comes from this part of the code:

....btnFind.ClientID...

Obviously, all of this assumes that each button is a server control
(Runat="Server").
It it posting the form for me?


I will do the same thing as when you physically click the button with the
mouse.

Jan 27 '06 #15
>I agree with Sean that you probably really don't need 4 buttons - just 1.
When the user clicks the one button just do a search using any field that
is populated - similar to the address search on MapQuest:

http://www.mapquest.com/


That makes sense in that all the fields are basically doing the same
thing...searching for an address.

In our interface, these field do completely different things. They are not
related other than they are all ways to get to different types of content on
the site.
It appears that when the search field is being editing, the onKeyPress
trigger calls the function.


Yes, when the search field has *focus* and you press [Enter], it will
click the button (object name) that you pass into the function. The
object name of your button comes from this part of the code:


This is perfect, then. I'll definitely give this a shot. What I was confused
was that I didn't see the onKeyPress specifically watching for the ENTER
key, but now that I look at it, that's exactly what the keyCode = 13 is.

Thanks!

-Darrel
Jan 27 '06 #16
Darrel,

This is a common problem that is most often solved with javascript.

A script like this added to each of your textboxes, of course changing the
button, will take care of things:

TextBox1.Attributes.Add("onkeydown", "javascript:if((event.which &&
event.which == 13) || (event.keyCode && event.keyCode ==
13)){document.getElementById('" & Button1.ClientID & "').click();return
false;}else return true;")

The script checks each time a key is pressed while the given textbox has
focus. If the key is then enter key it triggers the button click for the
given button.

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
"darrel" <no*****@nowhere.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
That won't work because the browser is automatically firing the Click
event for the Search button when he hits enter. Besides, I don't even
see a reason to use hidden fields, to begin with. Each individual button
can have an event handler in the server side code.


Right. The problem is really on the front end. By default, hitting return
triggers the first form submit button.

-Darrel

Jan 27 '06 #17
Hi,

darrel wrote:
If you're looking to have the appropriate button triggered based on
what field they're in, try javascript... but I would also rethink your
design.

I'm thinking that there just isn't a solution to this ;o)

The issue is that I need some sort of event in javascript to trigger focus
on the approporiate button. However, there isn't an event to trigger from.

-Darrel


In javascript: <input type="text"
onchange="this.form.<buttonName>.focus();" />

You can add this scriptlet to the ASP.NET Textfield using

protected TextBox tfTest;
(...)

// In OnInit
tfTest.Attributes.Add( "onblur",
"document.getElementById('bnOk').focus();" );

Note that it only works if your button has a ClientId "bnOk", so if
you're in a UserControl, the JavaScript code above might not work,
because then the ID might be something different. If you're in a User
control, you must pass the unique Client ID of the button to the
client,so that document.getElementById returns something... Complicated,
I know.

Anyway, it's not very nice, and what happens in JavaScript is disabled?
So actually, your server should check the Request.Form collection, for
the name of the button pressed (the name of the button pressed is sent
from the client to the server as part of the Form, while other
non-pressed buttons' name is omitted). This way, you can compare if the
filled textfield and the button pressed actually match. In short: If the
bnOkSearch is pressed, the Form collection will contain an entry with
this name, and will not contain bnOkZip.

Still not a good design, but at least it's foolproof.

Note that the "normal" way to handle this in web development would be to
use one form for the ZIP area and another form for the Search area.
Unfortunately, ASP.NET (at least in 1.1) only supports one form per
page, which is silly in my opinion.

HTH,
Laurent
--
Laurent Bugnion, GalaSoft
Software engineering: http://www.galasoft-LB.ch
Private/Malaysia: http://mypage.bluewin.ch/lbugnion
Support children in Calcutta: http://www.calcutta-espoir.ch
Jan 28 '06 #18
> Darrel,

This is a common problem that is most often solved with javascript.

A script like this added to each of your textboxes, of course changing the
button, will take care of things:

TextBox1.Attributes.Add("onkeydown", "javascript:if((event.which &&
event.which == 13) || (event.keyCode && event.keyCode ==
13)){document.getElementById('" & Button1.ClientID & "').click();return
false;}else return true;")


Thanks!

-Darrel
Jan 30 '06 #19
>> Anyway, it's not very nice, and what happens in JavaScript is disabled?
So actually, your server should check the Request.Form collection, for the
name of the button pressed (the name of the button pressed is sent from
the client to the server as part of the Form, while other non-pressed
buttons' name is omitted). This way, you can compare if the filled
textfield and the button pressed actually match.
I was going to do this, but the issue is that all of these forms are
actually rendered in different usercontrols, so checking each one became a
bit more complex.
Note that the "normal" way to handle this in web development would be to
use one form for the ZIP area and another form for the Search area.
Unfortunately, ASP.NET (at least in 1.1) only supports one form per page,
which is silly in my opinion.


I concur. ;o)

-Darrel
Jan 30 '06 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: actionwoman63 | last post by:
Dear all I need to be able to check which one out of two submit buttons within the same form was pressed in a javascript function prior to form submission. And before I get flamed for not...
7
by: M | last post by:
i have a form which i would like to input different "action" url depending on the button that was clicked. is there a way that javascript can prefill a defined action based on the button...
1
by: Norman Fritag | last post by:
Hi there, I have one subform in a Master form, which is set to add mode only. I allow only for data entry. Sofar all words fine. All information on the main form is set for display only and have...
3
by: Steve | last post by:
Hi, I want to determine which key was pressed in a control, like a datagrid. I see that in the currentcellchanged event of a datagrid I can capture keystroke events. I have tried someting...
6
by: stellstarin | last post by:
I have a HTML page containing two submit buttons in the same form.When the form is submitted,I want to know through which submit button the form was submitted. Is there any event or property which...
1
by: thewickedman | last post by:
Hi, I try to open new OpenOffice document (writer), But I am getting the exception FRM 40735:WHEN-BUTTON-PRESSED Trigger Raised unhandled Exception ORA - 305500 Please help me to resolve...
5
by: plumba | last post by:
Ok, another query.... I have a checkbox at the bottom of my form which when checked unhides a <div> block which displays the submit button. The problem I have is when the clear form button is...
3
by: MDR | last post by:
HI I'm writing a program in Visual C++. Among other things it does, when you press a button in the main window, it opens an auxiliar window crate as a Modal Dialog and let the user to choose...
8
by: jodleren | last post by:
Hi It is late and I am tired. I cannot remember how to check which of my buttons on the form was pressed. There are all submit's. Like echo "P=".$_POST; // and eventually using isset() ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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,...

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.