Form.DefaultButton stopped working in firefox

  • Thread starter Thread starter Tim Mackey
  • Start date Start date
T

Tim Mackey

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 button click handler, instead it fires the event for the bogus button
above it. btw it doesn't matter if i set it to ClientID, UniqueID or
"btnSubmit" hard-coded, they all fail.

<form id="form1" runat="server">
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click"
Text="Bogus" />
<asp:Panel ID="pnlSelect" runat="server">
<asp:TextBox ID="txtID" runat="server"
Columns="4"></asp:TextBox>
<asp:Button ID="btnSelect" runat="server"
OnClick="btnSelect_Click" Text="Submit" />
</asp:Panel>
</form>


public partial class test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
Page.Form.DefaultFocus = this.txtID.ClientID;
Page.Form.DefaultButton = this.btnSelect.UniqueID;
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Write("BOGUS BUTTON");
}

protected void btnSelect_Click(object sender, EventArgs e)
{
Response.Write("SUBMIT BUTTON");
}
}

thanks
tim
 
Tim Mackey said:
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 button click handler, instead it fires the event for the bogus button
above it. btw it doesn't matter if i set it to ClientID, UniqueID or
"btnSubmit" hard-coded, they all fail.
<snip>

Tim,

There are a couple of things going on. First, a form tag does not have
viewstate so if you want to set the DefaultButton/DefaultFocus in the
Page_Load event, you have to do it whether or not the page is posted back.

The other thing is that controls have 3 Id properties (ClientID - for client
side javascripts, UniqueID - used internally by the postback, and ID - Id of
the control as is in the designer). From your code sample, you would need to
use the ID property.

Try this code below.

protected void Page_Load(object sender, EventArgs e)
{
this.Page.Form.DefaultButton = btnSelect.ID;
this.Page.Form.DefaultFocus = txtID.ID;
// Could also use control.Focus().
//txtID.Focus();
}

If you view the Html page source in IE and Firefox you'll see that the
defaults are rendered properly both on the first page load and after a
postback. Something like:
....
<form name="form1" method="post" action="DefaultButton.aspx"
onkeypress="javascript:return WebForm_FireDefaultButton(event, 'btnSelect')"
id="form1">
....

When you execute the above code only on the initial page load (.. if
(!IsPostBack) {...}..) then you'll notice that the defaults are correctly
rendered on the html's form tag the first time the page is loaded. Then
after the first postback they are not there.

... after postback in the html page source for both ie and firefox ..
<form name="form1" method="post" action="DefaultButton.aspx" id="form1">
...

Personally, unless I need to control the defaults programmatically, I
usually just code the defaults in the page aspx like this:

<form id="form1" runat="server" defaultbutton="btnSelect"
defaultfocus="txtID">
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" ....
</form>

OR (DefaultButton on pnlSelect in case you have multiple panels on your page
or are using master page).

<form id="form1" runat="server" defaultfocus="txtID">
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click"
Text="Bogus" />
<asp:Panel ID="pnlSelect" runat="server" DefaultButton="btnSelect">
<asp:TextBox ID="txtID" runat="server" Columns="4"></asp:TextBox>
<asp:Button ID="btnSelect" runat="server"
OnClick="btnSelect_Click" Text="Submit" />
</asp:Panel>
</form>

Hope this helps,
Jason Vermillion
 
Thanks for Jason's informative input.

Hi Tim,

As Jason has suggested, for the Form.DefaultButton and Form.DefaultFocus
setting code, there are two places you need change:

1. You need to put the code that configure the HtmlForm.DefaultFocus and
DefaultButton in every reques(no only the initial request)

2. You should use "ID" property rather than "ClientID" or "uniqueID".
Actually, for server-side processing, generally we only need to use
Control's ID(the other twos are mostly used for client-side processing).

Here is the modified codebehind code snippet I've used correctly on my
side(win XP sp2, ie7 and ff2.0):

=====================

public partial class nav_NavButtonPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

Page.Form.DefaultButton = btnSelect.ID;
Page.Form.DefaultFocus = txtID.ID;


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

}
protected void btnSelect_Click(object sender, EventArgs e)
{
Response.Write("SUBMIT BUTTON");
}
}

===========================

Hope this also helps.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead



==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.



Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.

==================================================



This posting is provided "AS IS" with no warranties, and confers no rights.
 
hi guys,
many thanks for the detailed replies.
i was a bit thrown because i read in the HtmlForm.DefaultButton property in
the docs that "If you are using master pages in your application and you are
setting the DefaultButton property from a content page, use the UniqueID
property of the IButtonControl button", and assumed this would also work for
normal pages. i re-read it now and it does clearly say to use the ID
property first, so my bad, but it is slightly annoying that the behaviour
changes for master pages or normal pages, particularly because we can't use
the declarative syntax for child pages since the form tag is in the master
page.

thanks anyway, i can live with it!
tim
 
Thanks for your reply Tim,

Yes, you're right that for master page scenario, the "DefaultButton" and
"DefaultFocus" expect a different ID schema. For "DefaultButton" it expect
you to provide the Control.UniqueID while for "DefaultFocus", it expect you
to assign the Control.ClientID. Here is another thread I've been discussing
on this:

http://groups.google.com/group/microsoft.public.dotnet.framework.aspnet/brow
se_thread/thread/cd7c8c8ca73c3dac/ec126335ec8184eb

Hope this also helps.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top