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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

loosing control (me) over button keeping focus!! 4

Status
Not open for further replies.

Alcar

Programmer
Sep 10, 2001
595
US
Hi all,

I have a series of form elements, all asp.net controls on a webpage. Especially I have 4 buttons, three of wich do not trigger any validation.
Let's say:
Button1 (as the ok button)
Button2 (as the cancel button)
Button3 and Button4 (as other function buttons)

Problem: When I tab through my controls, Button3 continues to keep focus, therefore if I hit the Enter key, it triggers its click event.

I tried adding javascript to give and keep the focus on Button1. I tried giving tabIndex = 1 only to Button1 and 0 to all the rest. That button doesn't seem to lose it, while I am starting to lose my mind over it.

Still, when I tab through the controls, that button keeps that little blue shade on it.

Any suggestions?

Thanks
(loosing it... loosing it... LOST IT!)


Daren J. Lahey
Just another computer guy...
FAQ183-874 contains "Suggestions for Getting Quick and Appropriate Answers" to your questions.
Support your forums TODAY!
 
Hi Alcar,

From what I understand when you hit the 'Enter' key you want the OK button to fire and not the cancel or other buttons. Its fairly simple.

When the enter key is pressed on a form that has multiple submit buttons then the onclick event of the first button that appears in the form is fired. By 'first' button I mean when you select view source on the page, the first submit button of the form.

So, let's say you design a web form with multiple submit buttons. Goto the HTML view and move the 'OK' buttons html code to the beginning of the form. This will make sure that the OK button's onclick fires everytime you click the Enter key.

Hope this helps.
 
I see. But what if for a design purpouse I am forced to put the ok button at the bottom?


Daren J. Lahey
Just another computer guy...
FAQ183-874 contains "Suggestions for Getting Quick and Appropriate Answers" to your questions.
Support your forums TODAY!
 
I did finally fix it by changing the two buttons (Button3, Button4) in LinkButtons. This came afer your suggestion about the submit buttons. You deserve a star for that =)

Thanks.

Daren J. Lahey
Just another computer guy...
FAQ183-874 contains "Suggestions for Getting Quick and Appropriate Answers" to your questions.
Support your forums TODAY!
 
Hi Alcar,

If you are using a grid layout for your form, then if you move the 'OK' button in the HTML view as the first component in the form you should still be ok as the coordinates as the final position of the button on the screen is specified as attributed for the button.

However, if you use flow layout, then I see your problem. To work with flowlayout, you need to write custom javascript to bind input controls to a particular button, so that if enter is clicked the OK buttons on click is fired. I found this link which would be useful in this case

laters
 
Very interesting jr. I'd give you more stars if I could for just that info. Kudos for you.

thanks again.

Daren J. Lahey
Just another computer guy...
FAQ183-874 contains "Suggestions for Getting Quick and Appropriate Answers" to your questions.
Support your forums TODAY!
 
I have found a rather usefull control that allows you to set(at design time) which button will be fired on the enter event depending which control has the focus at that time. Let me see now where did I get that from... ah yes. here we are


The link is also available in our tidbits FAQ I believe

That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
Hey Daren, here's a better way. I found this while browsing the .NET help:

On any Windows Form you can designate a Button control to be the accept button, also known as the default button. Whenever the user presses the ENTER key, the default button is clicked regardless of which other control on the form has the focus. (The exceptions to this are when the control with focus is another button — in that case, the button with the focus will be clicked — or a multiline text box, or a custom control that traps the ENTER key.)

To designate the accept button in the designer

Select the form on which the button resides.
In the Properties window, set the form's AcceptButton property to the Button control's name.
To designate the accept button programmatically

Set the form's AcceptButton property to the appropriate Button control.
' Visual Basic
Private Sub SetDefault(ByVal myDefaultBtn As Button)
Me.AcceptButton = myDefaultBtn
End Sub

// C#
private void SetDefault(Button myDefaultBtn)
{
this.AcceptButton = myDefaultBtn;
}

// C++
private:
void SetDefault(Button * myDefaultBtn)
{
this->AcceptButton = myDefaultBtn;
}


Does this help?

-Gary
 
Hi Bigfoot,

This 'better way' is only applicable to Windows Forms and not to Web Forms. Web forms don't have such a property.

Laters,
 
The Default buttons link I posted gets around this web form limitation and does allow you to not only set a default button, but also if you wish to define which button is "Clicked" when Enter is pressed. That is depending on which control/textbox (in most cases) has focus a different button can be "Clicked"

That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
If you have trouble give me a shout. It was a little finicky why I tried it.

That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
Zarcom,

I have downloaded this code...now, it is using C to code and in the pages that I want to use it I am already using a .vb file and javascript in the html file...is this still workable and if so, how do I start using it?

Thanks,

DLC
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top