DefaultButton of html form

V

Veerle

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
 
G

Guest

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
 
V

Veerle

Try to RegisterHiddenField
Sub Page_Load(...)

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

End Sub

Thanks 4 the suggestion...
I tried:

ourForm.DefaultButton = defaultButtonUniqueID;
Page.ClientScript.RegisterHiddenField("__EVENTTARGET",
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...
 
G

Guest

Thanks 4 the suggestion...
I tried:

ourForm.DefaultButton = defaultButtonUniqueID;
Page.ClientScript.RegisterHiddenField("__EVENTTARGET",
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)
 
V

Veerle

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="" /
 
G

Guest

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="" /









- 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("__EVENTTARGET",
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/PermaLink.aspx?guid=3f96f2ee-331f-480a-81af-dd62c8f92c23
 
G

Guest

On Mar 26, 10:45 am, "Veerle" <[email protected]> wrote:

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...
 
V

Veerle


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.
 
G

Guest

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;
}
 
V

Veerle

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,968
Messages
2,570,154
Members
46,702
Latest member
LukasConde

Latest Threads

Top