473,385 Members | 1,506 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.

DefaultButton of html form

Hi,

When you use multiple asp:Buttons on one Form, then asp.net generates
html submit buttons for all of them. When you put your cursor in one
of the textfields of the form, the default submit button is the first
submit button of the form. So if you press enter, then the form is
submitted as if you pressed the first submit button of the form. But
sometimes, you want the default button to be the second or third
submit button of your form. No problem, you can set this by using the
DefaultButton property of the Form. Asp.net then generates an
attribute for the html form so that a javascript method makes sure
that the other submit button is used as default button (for when
someone presses enter). This generated attribute looks something like
this: onkeypress="javascript:return WebForm_FireDefaultButton(event,
'...')"

So far so good, only, Internet Explorer does not know we are altering
the default submit button to another submit button than the first one,
because this is done through javascript. So visually, it still draws
some kind of border round the first submit button, thinking this is
the default submit button. My question now is, how can I change this
behaviour so that Internet Explorer draws this extra border around the
real default button (set by javascript)?

Css doesn't seem to be much of a help, as IE6 doesn't support
"outline", ":focus", ":active" or any other stuff that you could use
to try and make things differently. So I guess that if there is a
solution, it should be something in javascript?

Any ideas?
Veerle

Mar 26 '07 #1
9 6739
On Mar 26, 9:30 am, "Veerle" <veerleve...@hotmail.comwrote:
Hi,

When you use multiple asp:Buttons on one Form, then asp.net generates
html submit buttons for all of them. When you put your cursor in one
of the textfields of the form, the default submit button is the first
submit button of the form. So if you press enter, then the form is
submitted as if you pressed the first submit button of the form. But
sometimes, you want the default button to be the second or third
submit button of your form. No problem, you can set this by using the
DefaultButton property of the Form. Asp.net then generates an
attribute for the html form so that a javascript method makes sure
that the other submit button is used as default button (for when
someone presses enter). This generated attribute looks something like
this: onkeypress="javascript:return WebForm_FireDefaultButton(event,
'...')"

So far so good, only, Internet Explorer does not know we are altering
the default submit button to another submit button than the first one,
because this is done through javascript. So visually, it still draws
some kind of border round the first submit button, thinking this is
the default submit button. My question now is, how can I change this
behaviour so that Internet Explorer draws this extra border around the
real default button (set by javascript)?

Css doesn't seem to be much of a help, as IE6 doesn't support
"outline", ":focus", ":active" or any other stuff that you could use
to try and make things differently. So I guess that if there is a
solution, it should be something in javascript?

Any ideas?
Veerle
Try to RegisterHiddenField

Sub Page_Load(...)

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

End Sub

Mar 26 '07 #2
Try to RegisterHiddenField
>
Sub Page_Load(...)

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

End Sub
Thanks 4 the suggestion...
I tried:

ourForm.DefaultButton = defaultButtonUniqueID;
Page.ClientScript.RegisterHiddenField("__EVENTTARG ET",
defaultButtonUniqueID);

in the prerender of the masterpage, which is where the DefaultButton
code already was. But this doesn't do much: when I look in the
generated html, I see:

<input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" /
>
but i expected to find the uniqueid of my button in the value
attribute.

And suppose that the hidden field contained the unique id of my
overrided default button, I don't see how Internet Explorer is going
to see that this is this is the submit button he has to draw the black
outline (visuallising the default button) around...

Mar 26 '07 #3
On Mar 26, 10:17 am, "Veerle" <veerleve...@hotmail.comwrote:
Try to RegisterHiddenField
Sub Page_Load(...)
Page.RegisterHiddenField("__EVENTTARGET", "Button2")
End Sub

Thanks 4 the suggestion...
I tried:

ourForm.DefaultButton = defaultButtonUniqueID;
Page.ClientScript.RegisterHiddenField("__EVENTTARG ET",
defaultButtonUniqueID);

in the prerender of the masterpage, which is where the DefaultButton
code already was. But this doesn't do much: when I look in the
generated html, I see:

<input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" /
Well, but what the defaultButtonUniqueID is?

I forgot that the UniqueID might help

If you have a button named "Button2"

try

Page.RegisterHiddenField("__EVENTTARGET", Button2.UniqueID)

(or check the name of the button through the rendered html code)

Mar 26 '07 #4
I tried this: Page.RegisterHiddenField("__EVENTTARGET",
Button2.UniqueID)
And I checked using a breakpoint that Button2.UniqueID is correct, and
it is.
But in the generated html, the hidden field is not filled in:

<input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" /
>


On 26 mrt, 10:29, "Alexey Smirnov" <alexey.smir...@gmail.comwrote:
On Mar 26, 10:17 am, "Veerle" <veerleve...@hotmail.comwrote:
Try to RegisterHiddenField
Sub Page_Load(...)
Page.RegisterHiddenField("__EVENTTARGET", "Button2")
End Sub
Thanks 4 the suggestion...
I tried:
ourForm.DefaultButton = defaultButtonUniqueID;
Page.ClientScript.RegisterHiddenField("__EVENTTARG ET",
defaultButtonUniqueID);
in the prerender of the masterpage, which is where the DefaultButton
code already was. But this doesn't do much: when I look in the
generated html, I see:
<input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" /

Well, but what the defaultButtonUniqueID is?

I forgot that the UniqueID might help

If you have a button named "Button2"

try

Page.RegisterHiddenField("__EVENTTARGET", Button2.UniqueID)

(or check the name of the button through the rendered html code)

Mar 26 '07 #5
On Mar 26, 10:45 am, "Veerle" <veerleve...@hotmail.comwrote:
I tried this: Page.RegisterHiddenField("__EVENTTARGET",
Button2.UniqueID)
And I checked using a breakpoint that Button2.UniqueID is correct, and
it is.
But in the generated html, the hidden field is not filled in:

<input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" /

On 26 mrt, 10:29, "Alexey Smirnov" <alexey.smir...@gmail.comwrote:
On Mar 26, 10:17 am, "Veerle" <veerleve...@hotmail.comwrote:
Try to RegisterHiddenField
Sub Page_Load(...)
Page.RegisterHiddenField("__EVENTTARGET", "Button2")
End Sub
Thanks 4 the suggestion...
I tried:
ourForm.DefaultButton = defaultButtonUniqueID;
Page.ClientScript.RegisterHiddenField("__EVENTTARG ET",
defaultButtonUniqueID);
in the prerender of the masterpage, which is where the DefaultButton
code already was. But this doesn't do much: when I look in the
generated html, I see:
<input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" /
Well, but what the defaultButtonUniqueID is?
I forgot that the UniqueID might help
If you have a button named "Button2"
try
Page.RegisterHiddenField("__EVENTTARGET", Button2.UniqueID)
(or check the name of the button through the rendered html code)- Hide quoted text -

- Show quoted text -
Okay, I've created a test page, placed two TextBox and two Button
Controls on it and added the following code

protected void Page_Load(object sender, EventArgs e)
{
//Page.ClientScript.RegisterHiddenField("__EVENTTARG ET",
Button2.UniqueID);
Page.Form.DefaultButton = Button2.UniqueID;
}

protected void Button1_Click(object sender, EventArgs e)
{
Response.Write("Button1");
}

protected void Button2_Click(object sender, EventArgs e)
{
Response.Write("Button2");
}

When Enter is pressed I see "Button2" on the page.

This is about using the DefaultButton property (it seems to work)

My first suggestion to use the RegisterHiddenField is not working when
you have two TextBoxes on the page, it works properly with one TextBox
and two Buttons only. More about that issue you can find here
http://www.hanselman.com/blog/PermaL...f-dd62c8f92c23

Mar 26 '07 #6
On Mar 26, 11:36 am, "Alexey Smirnov" <alexey.smir...@gmail.com>
wrote:
On Mar 26, 10:45 am, "Veerle" <veerleve...@hotmail.comwrote:
Sorry, forgot to mention

the focus seems strange, however, until you don't click on any text
field you can properly set it with the TabIndex Property

Button2.TabIndex = 0;
Button1.TabIndex = 1;

although, when TextBox is "clicked" IE changes the focus to the first
button again...

Mar 26 '07 #7
On 26 mrt, 11:36, "Alexey Smirnov" <alexey.smir...@gmail.comwrote:
More about that issue you can find here http://www.hanselman.com/blog/PermaL...2ee-331f-480a-...
This is discussion from the year 2004, from before .NET 2.0. They seem
to be discussing the issue of what happens when you press enter and
how to control that. Now that we have .NET 2.0, we can use the
DefaultButton property to do this. So I just need to get the black
border around the default button that was set with DefaultButton.

Mar 26 '07 #8
On Mar 26, 12:02 pm, "Veerle" <veerleve...@hotmail.comwrote:
On 26 mrt, 11:36, "Alexey Smirnov" <alexey.smir...@gmail.comwrote:
More about that issue you can find herehttp://www.hanselman.com/blog/PermaLink.aspx?guid=3f96f2ee-331f-480a-...

This is discussion from the year 2004, from before .NET 2.0. They seem
to be discussing the issue of what happens when you press enter and
how to control that. Now that we have .NET 2.0, we can use the
DefaultButton property to do this. So I just need to get the black
border around the default button that was set with DefaultButton.
I think I got it.

protected void Page_Load(object sender, EventArgs e)
{
Button1.UseSubmitBehavior = false;
Button2.UseSubmitBehavior = true;

Page.Form.DefaultFocus = Button2.ClientID;
}

Mar 26 '07 #9
This seems to work very well indeed! Thanks a lot for helping out!

I was already wondering why ASP.NET generated submit buttons instead
of regular buttons, because they manage the real postback using
javascript which can as well be done with regular buttons. But I
didn't know there was property to change this.

On 26 mrt, 12:56, "Alexey Smirnov" <alexey.smir...@gmail.comwrote:
On Mar 26, 12:02 pm, "Veerle" <veerleve...@hotmail.comwrote:
On 26 mrt, 11:36, "Alexey Smirnov" <alexey.smir...@gmail.comwrote:
More about that issue you can find herehttp://www.hanselman.com/blog/PermaLink.aspx?guid=3f96f2ee-331f-480a-...
This is discussion from the year 2004, from before .NET 2.0. They seem
to be discussing the issue of what happens when you press enter and
how to control that. Now that we have .NET 2.0, we can use the
DefaultButton property to do this. So I just need to get the black
border around the default button that was set with DefaultButton.

I think I got it.

protected void Page_Load(object sender, EventArgs e)
{
Button1.UseSubmitBehavior = false;
Button2.UseSubmitBehavior = true;

Page.Form.DefaultFocus = Button2.ClientID;

}

Mar 26 '07 #10

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

Similar topics

0
by: Jeff | last post by:
We have 3 environments, Dev, Qa, Prod. I'm working on an application that has a search page. On the search page is 3 panels. One panel is the advanced search, another a normal search and the...
0
by: Evert Wiesenekker | last post by:
I have a mail page with a send button containing three validators (2 required val. and 1 expression val.). The masterpage contains 3 imagebuttons. Now when I press enter the first time the...
2
by: Oz Kologlu | last post by:
Hi folks, IE and Asp.net 2.0 are doing some pretty bizzare things. Some of our users were getting pretty weary of IE redrawing and "flashing" what is essentially a pretty static page every time...
1
by: gferreri | last post by:
Hi all, I've stumbled on an interesting problem with the way Firefox handles form submitting with the enter key. I'm putting together a page that has one form element with multiple controls...
7
by: Tim_Mac | last post by:
this is a re-post of an earlier message. i'm posting it under my MSDN alias for a better chance of reply :) when you press return in a multiline textbox, you expect to insert a newline. ...
3
by: John Mott | last post by:
Hi All, I'm trying to set the defaultbutton for a form to a button contained in a step template for a wizard control. This is the code in PreRender: switch (myWizard.ActiveStep.StepType) {...
2
by: =?Utf-8?B?QmlnSm9obg==?= | last post by:
We can set the default button and focus field in ASP.Net 2.0 on the Form tag. My form tag is in the Master page, but the controls are in containers. I read through a few articles which did not...
4
by: Tim Mackey | last post by:
hi, asp.net 2. can anyone explain why this code does not work in firefox (2.0.0.1), but does work in IE 7. if you hit enter after typing something into the textbox, it should fire the Submit...
0
by: =?Utf-8?B?QWxleCBNYWdoZW4=?= | last post by:
Hi. I have a MasterPage but on each particular page that uses it, I need to be able to set the DefaultButton for the Form for that page. From the .cs file of my ASPX page, I have tried: -...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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?
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...

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.