"Tim Mackey" wrote:
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