One of the most annoying things in developing web pages is handling the "Enter key" for form submission. Enter key has been the favourite way users like to submit forms. Though we provide Buttons to click on,
the easiest and intuitive way is that, I can enter some text, make some changes and then hit "Enter" to accomplish my submission.
"Enter" Key is handled in a little tricky way by uplevel browsers like Internet Explorer, when it comes to ASP.NET.
If there is a single Textbox and single button, then it becomes straight forward, the button is submitted. However, the event code doesnt get executed, though the page postsback.
If there are two or more, buttons, then it takes up the first button as the default button. However, it still doesnt execute the event handler but just refreshes the page.
You can supress the Enter key event using Javascript. But this would result in other undesirable effects like, any Enter key in the form i.e. within Text Area or basically where large text is entered, would be
disabled.
The earlier work around was to associate a javascript function to each Button to verify the that the relevant button is submitted upon Enter key.
ASP.NET 2.0 introduces a wonderful work around for this. By simply specifying the "defaultbutton" property to the ID of the <asp:Button>, whose event you want to fire, your job is done.
The defaultbutton
property can be specified at the Form level in the form tag as well as at panel level in the <asp:panel> definition tag. The form level setting is overridden when specified at the panel level, for those controls that are inside the panel.
Also, the Event Handler for the specified button, fires thereby simulating a true submit button functionality.
The following sample code contains a form and 4 panels with each of them containing different buttons. It can be noticed that for each panel, there is a default button specified which would trigger the corresponding
button's event handler when "Enter" Key is pressed upon a text changed event.
Once we execute the above functionality, we can notice, the corresponding Buttons' text are displayed when the Enter key is pressed from within a panel and at the form level, it fires the
btn1 Button's event.
I can hardly see anything 'wonderful' about this. It is a fudge. There is a BUG, to use Microsoft's own words and instead of fixing it they give you the option of deciding which way the bug is going to work. Why should there be ANY action on pressing the ENTER
key outside the context of the control (textbox) without the programmer specifically including it? I do not want any action on Enter. Too many people, including myself, still use this key at the end of every entry in a textbox when, apart from trapping it
for validation purposes, it should merely send the user to the next field. Is this why so many data entry forms I use on the web include only one item on each of a multitude of pages?
The main aim of the defaultButton is to ensure Enter Key Submissions. Typically people are very much used to Enter Key submission and if we disable it might be annoying certain times.
The defaultButton really provides a way to handle Enter Keys for respective portions of the page when the focus is there.
> The main aim of the defaultButton is to ensure Enter Key Submissions. Typically people are very much used to Enter Key submission and if we disable it might be annoying certain times.
Sorry, but i don't think so. A form submitted with an enter key is on the contrary a problem you encounter any time you have a *single* textbox in a form, and it is a legacy behaviour you usually have to... disable. Now the Fwk has a defaultButton property,
but that outputs some javascript to handle key events and act accordingly, so this is something *more* than simply dealing with this legacy behaviour with enter keys. That is, i would at least combine them both: first, disable legacy behaviour, then eventually
add support for enter key submission...
As [hondav] is telling:
> There is a BUG, to use Microsoft's own words and instead of fixing it they give you the option of deciding which way the bug is going to work.
So, great to have a defaultButton and great you have posted a sample usage. Yet i believe this should be unrelated to the legacy behaviour we are talking about.
I have this same question. Even worse is I have an asp:login which iselt creates the button. So how can I set the ASP.Net 2.0 Login window to fire the button click on Enter? And it's in a asp:content
I have a page with two forms, one with just one textbox, and the other with many textboxes, dropdownlists, checkboxes, etc. This is a search page, with the results showing up in popups. The basic requirement is that if the user types in a value in the first
textbox and hits the ENTER key, the first form is submitted and a popup shows the results of the search. If the user fills in any fields in the second part of the form and hits ENTER on any textbox, the second form is submitted and the results are popped
up. The two popups are different, obviously.
I've been struggling with how to capture the ENTER key and send the user one or the other result popup. Then someone directed me to your article here. I inserted two panels enclosing the two different sets of controls into the panels. I set the defaultbutton
for the panels and tried it out. It works the first time I hit the ENTER key on any field. But if the user hits ENTER again, the first form is ALWAYS submitted. So, for example, if they didn't get the restuls they expected and went back to the main form
and added more criteria and hit ENTER, it would submit the first form, which is incorrect. The user has to refresh the page to get the second form to submit on ENTER (or click the submit button). This won't work for us. I'm wondering if this is a bug, and
if so is there a workaround? if not, what could I be missing to cause this behavior?
Thanks so much for your help - this webapp is nearly done, but this issue is causing me a lot of headaches! Hopefully this will be resolved soon.
I have a page with two forms, one with just one textbox, and the other with many textboxes, dropdownlists, checkboxes, etc. This is a search page, with the results showing up in popups. The basic requirement is that if the user types in a value in the first
textbox and hits the ENTER key, the first form is submitted and a popup shows the results of the search. If the user fills in any fields in the second part of the form and hits ENTER on any textbox, the second form is submitted and the results are popped
up. The two popups are different, obviously.
I've been struggling with how to capture the ENTER key and send the user one or the other result popup. Then someone directed me to your article here. I inserted two panels enclosing the two different sets of controls into the panels. I set the defaultbutton
for the panels and tried it out. It works the first time I hit the ENTER key on any field. But if the user hits ENTER again, the first form is ALWAYS submitted. So, for example, if they didn't get the restuls they expected and went back to the main form
and added more criteria and hit ENTER, it would submit the first form, which is incorrect. The user has to refresh the page to get the second form to submit on ENTER (or click the submit button). This won't work for us. I'm wondering if this is a bug, and
if so is there a workaround? if not, what could I be missing to cause this behavior?
Thanks so much for your help - this webapp is nearly done, but this issue is causing me a lot of headaches! Hopefully this will be resolved soon.
Thanks again,
eddie
I'm having the same problem with two
controls on the same page (Signin & Signup)
I wonder if this was forgotten when creating ASP.NET coz I can not thing on any "EZ workaround" (as it was so EZ 2 implement on classic ASP)
So now - whenever the control area is being clicked (or focused) or any control inside it - the default button will used,
It will not go to a default button if the focus is outside of these two controls (for example - on the web site header) - the following script might also B useful:
<script language="javascript">
// <!--
function onKeySignin()
{
if (window.event.keyCode == 13)
{
document.getElementById('<%=_buttonSignin.ClientID%>').focus();
document.getElementById('<%=_buttonSignin.ClientID%>').click();
}
}
After much seraching, I found a workable solution to this problem. Basically we need to manually set up the same client code that would be injected if we were using a Default button for a panel.
Assume we have a <asp:Login ... /> control on a page named "Login1"
In the Page_Load event handler:
------
Control ctl = Login1.FindControl("LoginButton");
if(ctl != null && ctl
is IButtonControl)
Login1.Attributes.Add("onkeypress",
string.Format("javascript:return WebForm_FireDefaultButton(event, '{0}')",ctl.ClientID));
-------
I'm assuming that we also have the WebForm_FireDefaultButton client script file available on the page... (if not - you'll get a JavaScript error)
People go through periods in the lives - I'd rather go through exclamation points.
- Schultz
Hi, In my site also this is the problem. in my site i am using master pages and in the master page i have a usercontrol in the usercontrol i have an image button. and when i go to login page in that login page i used a login control. when i open login page
and enter the user name and password and hit enter then the search button gets fired. so i searched for this in the forms but i didint find any information regarding this. so can you please help me regarding this.
Folks, you have to remember that not everyone is using old login controls. We are using the new .NET 2.0 Membership API. Thus .NET provides the button and textboxes. Plus, if you're using master pages, you can't simply play around with defaultbutton because
what button are you referring to if you are using a master page? that is not possible as differrent detail pages will have it's own button!
This is something you should be able to do. I had a boss who insisted on it, rather than just disabling the enter key. It should be textbox driven though, its not a good idea to use Panels just for this purpose, unless you use them anyway for other reasons.
So this can be used at the FORM level and PANEL level. Where else? Textbox (or other control) would be ideal.
<asp:TextBox defaultbutton="soandso">
You may have a reason to point one textbox to one button and another textbox to another button.
how we set this defaultbutton property to textbox, because there is no such propety in the textbox control. and one more thing i am using asp.net 2.0 login view control. can you please suggest me in this.
For those who are using the .Net 2.0 login control, I just thought about a hacky way to implement the scenario where users can hit the Enter key to fire the login button event, here is how it works:
1- wrap your login control into a Panel and call it Panel1.
2- inset a button on stage and call it dummyButton, set it's width and height to 0, set it border to none and it is background anad foreground to white set it is text to empty string.( this is way, dummybutton will be invisible). If you are wondering why
wouldn't I just set the visibility to false, well the answer is that it won't work.
3- set the defaultbutton of Panel1 to dummyButton and set put this code the dummyButton your click handler
Dont forget to click "Mark as Answer" on the post that helped you.
This credits that member, earns you a point and marks your thread as Resolved so we will all know you have been helped.
After much seraching, I found a workable solution to this problem. Basically we need to manually set up the same client code that would be injected if we were using a Default button for a panel.
Assume we have a <asp:Login ... /> control on a page named "Login1"
In the Page_Load event handler:
------
Control ctl = Login1.FindControl("LoginButton");
if(ctl != null && ctl
is IButtonControl)
Login1.Attributes.Add("onkeypress",
string.Format("javascript:return WebForm_FireDefaultButton(event, '{0}')",ctl.ClientID));
-------
I'm assuming that we also have the WebForm_FireDefaultButton client script file available on the page... (if not - you'll get a JavaScript error)
Not sure why you guys are still having problems after this post. I'm using .Net 2.0 Login controls and this worked great for me. I'm using VB thought I coudln't translate the If Then check to see if it's a button and null, but the rest of it worked great.
What's the reason for all this Panel1 and invisible buttons for?
My Code Behind, just like the post:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim ctl As Control = userLogin.FindControl("LoginButton")
userLogin.Attributes.Add("onkeypress", String.Format("javascript:return WebForm_FireDefaultButton(event, '{0}')", ctl.ClientID))
End Sub
I chased this issue a lot and think I understand it now. I had a content page with 2 usercontrols. One contained a textbox for zipcode and a Find button. The 2nd user control contained a LoginView and Login control, the latter of which generates textboxes
for username and password and a button for Login. There were numerous difficulties getting this to work but now the user can enter text in either box and hit Enter. The context is determined and the proper button is fired (without an explicit click of the
desired button nor moving focus to the desired button). Here is the code in VB.Net:
Dim myLogin As Login = CType(Me.LoginView1.FindControl("Login1"
<div id="intelliTxt">), Login)
If myLogin IsNot Nothing Then
Dim myButton As Button = CType(myLogin.FindControl("LoginButton"), Button)
If myButton IsNot Nothing Then
Me.Page.Form.DefaultButton = myButton.UniqueID
Dim myFoundTextBox As TextBox = CType(myLogin.FindControl("Password"), TextBox)
myFoundTextBox.Attributes.Add("onclick", "document.forms[0].onkeypress = " _
+ "new Function(""return WebForm_FireDefaultButton(event, '" + myButton.UniqueID + "');"");")
End If
End If
And in my FindZipCodeNearYou.ascx user control I have the following:
Dim myButton As Button = CType(Me.FindControl("btnFind"), Button)
If myButton IsNot Nothing Then
Me.Page.Form.DefaultButton = myButton.UniqueID
Dim myFoundTextBox As TextBox = CType(Me.FindControl("txtZipCode"), TextBox)
myFoundTextBox.Attributes.Add("onclick", "document.forms[0].onkeypress = " _
+ "new Function(""return WebForm_FireDefaultButton(event, '" + myButton.UniqueID + "');"");")
End If
Both of these user controls are used on the same content page within a master page. Now in either textbox above, the user can type his password (or zipcode) and immediately hit the ENTER key and the proper button will be fired.</div>
defaultButton onkeypress firedefaultbutton enter key
I wanted to mention that the Login control was the easiest to get working with some of the above solutions using the javascript:return WebForm_FireDefaultButton stuff or you can do this... (check for nulls if you want... I didn't include
that here)
However my problem was figuring out how to reference textboxes and buttons in other controls like the Password Recovery Control and the Create User Wizard Control. Something like... PasswordRecovery1.FindControl("UserName").UniqueID will not return
the username textbox like it does for the Login Control. The UserName control is hiding in some containers (which I can't seem to find the name from anywhere in VS... anyone know where this is?). I only found the name for the control by using View Source
in my browser.
I was able to come up with this for the Password Recovery Control:
The default button seems to be already set on the Create User Wizard.
I'm mainly just posting this for people searching for the solution for the controls other than the Login because I spend hours yesterday trying to reference these controls until my brain clicked on and I decided to use View Source to get the
references for the controls.
Goodluck!
Default ButtonLogin ControlCreate User WizardDefaultFocusSet FocusPassword Recovery
Tagging this for later. A bit hackish, but it certainly solved my problem when a usercontrol had a search box in a child contol as well as a login box.
Looking at code you wrote more than two weeks ago is like looking at code you are seeing for the first time.
— Alzheimer's Law of Programming
I placed this code in the Page_Load event handler. I even stepped through the code and it *seemed* to work correctly but when the page actually loaded, only the Default button was set but the cursor was definitely not in the UserName textbox.
Now, my login control is sitting inside of a couple of panels which are sitting in a content page (ie. connected to a master page). Might this have something to do with it?
I just discovered that Ellipse's code is working. The "problem" is that my login control resides inside of a panel that has the Ajax Control Toolkit's RoundedCornersExtender improving its appearance. So though the cursor is being correctly focused, then
when the Ajax control does its work, the focus is lost.
I'm going to place a question on a different forum to try to find an answer for this!
But then the next step (asking to answer the secret question), if I hit the enter key after answered the question, it won't work... I have some textbox and another submit button on the page for other purposes. I've tried to add this:
I'm not 100% sure but I think your code may be correct but it's just located in the wrong place. Here's the code I implemented, which I still need to extensively test:
protected void Page_Load(object sender, EventArgs e)
{
// Make the Submit button the default button and place the cursor in the User Name textbox
if (!IsPostBack)
{
Page.Form.DefaultButton = PasswordRecovery1.FindControl("UserNameContainerID$SubmitButton").UniqueID;
Page.Form.DefaultFocus = PasswordRecovery1.FindControl("UserNameContainerID$UserName").UniqueID; // Doesn't currently work because of Ajax RoundedCornersExtender
}
}
// This is called after the User Name is entered; we'll use it to change the default controls
protected void PasswordRecovery1_VerifyingUser(object sender, LoginCancelEventArgs e)
{
Page.Form.DefaultButton = PasswordRecovery1.FindControl("QuestionContainerID$SubmitButton2").UniqueID;
Page.Form.DefaultFocus = PasswordRecovery1.FindControl("QuestionContainerID$Answer").UniqueID; // Doesn't currently work because of Ajax RoundedCornersExtender
}
The second event handler is being called because of this: <asp:PasswordRecovery ID="PasswordRecovery1" runat="server" OnVerifyingUser="PasswordRecovery1_VerifyingUser">
Hi,Thank you, this is what I was looking for – enter key for form submission. But, the ValidationSummary doesn’t get popped up, when enter key is pressed in the text box, it gets popped up only on click of submit button.Please suggest me how to make the ValidationSummary to get popped up when the enter key is pressed in the text box.
I'm not sure why it's not working as you feel it should.
Now, I don't use the ValidationSummary control (just don't like it) but take a look at this page: http://pocketpollster.com/beta
On the right side there are two RequiredFieldValidator controls and one RegularExpressionValidator control. Place the cursor inside either textbox and press Enter. You'll notice that the validators are fired.
Note: This Beta site isn't yet wired up with SQL Server so it's far from fully functional.
Protected Sub Page_Load(ByVal sender
As Object, ByVal e
As System.EventArgs) Handles Me.Load
If Not IsPostBack Then Me.Form.DefaultButton = PasswordRecovery1.UserNameTemplateContainer.FindControl("SubmitButton").UniqueID CType(PasswordRecovery1.UserNameTemplateContainer.FindControl("UserName"), TextBox).Focus() End If
End Sub
Protected Sub PasswordRecovery1_VerifyingUser(ByVal sender
As Object, ByVal e
As System.Web.UI.WebControls.LoginCancelEventArgs) Handles PasswordRecovery1.VerifyingUser
Re: ASP.NET 2.0 - Enter Key - Default Submit Button
Jan 04, 2008 06:34 AM|coolbear_lq@hotmail.com|LINK
bachbouch
For those who are using the .Net 2.0 login control, I just thought about a hacky way to implement the scenario where users can hit the Enter key to fire the login button event, here is how it works:
1- wrap your login control into a Panel and call it Panel1.
2- inset a button on stage and call it dummyButton, set it's width and height to 0, set it border to none and it is background anad foreground to white set it is text to empty string.( this is way, dummybutton will be invisible). If you are wondering why
wouldn't I just set the visibility to false, well the answer is that it won't work.
3- set the defaultbutton of Panel1 to dummyButton and set put this code the dummyButton your click handler
I think I have a even better solution for the Default Enter Key in Login Control. You don't need to write any code. Just follow the following steps:
1. Convert the Login control into a template by selecting the control and clicking the the arrow button on the top right corner and select "Convert to Template".
2. In your Login control template design view, add a Panel into the template and put everything in the Login control into the Panel.
3. When you convert your Login control to a template, the submit button in the Login control is now separated out. Select on the submit button to find out its name and fill the name into the DefaultButton property of the Panel inside the Login control template.
I think I have a even better solution for the Default Enter Key in Login Control. You don't need to write any code. Just follow the following steps:
1. Convert the Login control into a template by selecting the control and clicking the the arrow button on the top right corner and select "Convert to Template".
2. In your Login control template design view, add a Panel into the template and put everything in the Login control into the Panel.
3. When you convert your Login control to a template, the submit button in the Login control is now separated out. Select on the submit button to find out its name and fill the name into the DefaultButton property of the Panel inside the Login control template.
Problem solved.
This does the job with indead minimal coding.
To set the focus to the username textbox I added the following code to the Page_Load:
hi everyone , I am also having the same problem and tried all the solutions but they r working only for single text box.if i set defaultbutton property of a form then it works for first text box only. also i have tried other solutions ,plz suggest some solution.[Idea]
I to face this problem in program. but i'm using master page. when i open another webform and enter the hit button it executes the master page button , but it supposed to execute current webform button
No Need Of this much of stuff.Just u can do in the following way
//the button which you want as default button define it in defaultbutton
<
form id="form1"
runat="server"
defaultbutton=
"btn_Submit">
B. Shiva Karthik
Mumbai , India
-------------------------------------------------
Don't Forget to Mark as Answer for the post that helped you.
-------------------------------------------------
I tried this and copied the name of the button into the panel button. I got the following error: The DefaultButton of 'Panel1' must be the ID of a control of type IButtonControl. Description: An unhandled exception occurred during the execution of the current
web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.InvalidOperationException: The DefaultButton of 'Panel1' must be the ID of a control of type IButtonControl I copied
the exact name, LoginButton and now I get this error.
Any one create more then one submit button in the application. Here is the Example. That create more then one submit button depending on the focus of the text button before hit enter.
Call makeSubmitButton on the page_Load and pass the textBoxId and also ButtonId which you want to make submit button when focus is on txtBoxId.
Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Regards...
Pradeep Kr. Sharma
Please remember to "Mark as Answer" the responses that resolved your issue.
Try using this, Assuming that we have asp TextBox with an ID of "TextBox1" and Button with an ID of "Button1" controls on our page.
Put this code on the Head of your aspx page.
<script language="javascript">
function doClick(e, buttonid){
var evt = e ? e : window.event;
var bt = document.getElementById(buttonid);
if (bt){
if (evt.keyCode == 13){
bt.click();
return false;
}
}
}
</script>
And this on page load event.
TextBox1.Attributes.Add("onkeypress", "return doClick(event,'" + Button1.ClientID + "')");
Using a Panel's "DefaultButton" worked perfectly for my needs. I have three text box functions on one page, and just made sure each "tool" had a Panel surrounding it, then added the button as the Default to each panel. Thanks! [Yes]
not sure if I've missed something with the problem, but I have a textbox on my form and everytime I hit enter, the form would accept the changes to the yexybox and process the order.
My solution was to include a 'fake' button that did nothing, something like this
I set visible=false at first but that didn't seam to work, so setting everything to the same colour as the background (i.e. white in my case) and that did the trick.
Member
182 Points
2336 Posts
Microsoft
ASP.NET 2.0 - Enter Key - Default Submit Button
Apr 27, 2006 11:31 AM|ranganh|LINK
Hi,
One of the most annoying things in developing web pages is handling the "Enter key" for form submission. Enter key has been the favourite way users like to submit forms. Though we provide Buttons to click on, the easiest and intuitive way is that, I can enter some text, make some changes and then hit "Enter" to accomplish my submission.
"Enter" Key is handled in a little tricky way by uplevel browsers like Internet Explorer, when it comes to ASP.NET.
You can supress the Enter key event using Javascript. But this would result in other undesirable effects like, any Enter key in the form i.e. within Text Area or basically where large text is entered, would be disabled.
The earlier work around was to associate a javascript function to each Button to verify the that the relevant button is submitted upon Enter key.
ASP.NET 2.0 introduces a wonderful work around for this. By simply specifying the "defaultbutton" property to the ID of the <asp:Button>, whose event you want to fire, your job is done.
The defaultbutton property can be specified at the Form level in the form tag as well as at panel level in the <asp:panel> definition tag. The form level setting is overridden when specified at the panel level, for those controls that are inside the panel.
Also, the Event Handler for the specified button, fires thereby simulating a true submit button functionality.
The following sample code contains a form and 4 panels with each of them containing different buttons. It can be noticed that for each panel, there is a default button specified which would trigger the corresponding button's event handler when "Enter" Key is pressed upon a text changed event.
<form id="form1" runat="server" defaultbutton="btn1">
<div>
<asp:TextBox ID="txt" runat="server"></asp:TextBox>
<asp:Button ID="Button5" runat="server" Text="Cancel" OnClick="Button5_Click" />
<asp:Button ID="btn1" runat="server" Text="Submit" OnClick="btn1_Click" />
<asp:Panel ID="pnl1" runat="server" defaultbutton="Button1">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button1" OnClick="Button1_Click" />
</asp:Panel>
<asp:Panel ID="Panel1" runat="server" defaultbutton="Button2">
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
<asp:Button ID="Button2" runat="server" Text="Button2" OnClick="Button2_Click" />
</asp:Panel>
<asp:Panel ID="Panel2" runat="server" defaultbutton="Button3">
<asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox6" runat="server"></asp:TextBox>
<asp:Button ID="Button3" runat="server" Text="Button3" OnClick="Button3_Click" />
</asp:Panel>
<asp:Panel ID="Panel3" runat="server" defaultbutton="Button4">
<asp:TextBox ID="TextBox7" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox8" runat="server"></asp:TextBox>
<asp:Button ID="Button4" runat="server" Text="Button4" OnClick="Button4_Click" />
</asp:Panel>
</div>
</form>
The corresponding, sample events for the button clicks are
protected void Button1_Click(object sender, EventArgs e)
{
Response.Write(Button1.Text);
}
protected void Button2_Click(object sender, EventArgs e)
{
Response.Write(Button2.Text);
}
protected void Button3_Click(object sender, EventArgs e)
{
Response.Write(Button3.Text);
}
protected void Button4_Click(object sender, EventArgs e)
{
Response.Write(Button4.Text);
}
protected void btn1_Click(object sender, EventArgs e)
{
Response.Write(btn1.Text);
}
protected void Button5_Click(object sender, EventArgs e)
{
Response.Write(Button5.Text);
}
Once we execute the above functionality, we can notice, the corresponding Buttons' text are displayed when the Enter key is pressed from within a panel and at the form level, it fires the btn1 Button's event.
Thanks.
Harish
http://geekswithblogs.net/ranganh
None
0 Points
12 Posts
Re: ASP.NET 2.0 - Enter Key - Default Submit Button
Jun 10, 2006 10:18 AM|shuaiqy|LINK
Member
2 Points
83 Posts
Re: ASP.NET 2.0 - Enter Key - Default Submit Button
Jun 14, 2006 04:30 PM|Rohit Gupta|LINK
You can also define the defaultbutton property in a panel as well ...
RG
None
0 Points
3 Posts
Re: ASP.NET 2.0 - Enter Key - Default Submit Button
Jun 26, 2006 03:43 AM|hondav|LINK
Contributor
2292 Points
1920 Posts
Re: ASP.NET 2.0 - Enter Key - Default Submit Button
Aug 04, 2006 11:57 PM|LudovicoVan|LINK
Indeed, there's no need to do all this work.
Check this topic for a clean approach: http://forums.asp.net/thread/1361423.aspx
HTH. -LV
None
0 Points
4 Posts
Re: ASP.NET 2.0 - Enter Key - Default Submit Button
Aug 07, 2006 05:03 AM|dabas|LINK
hi ranghan
Can you tell me how can i do that the same in version 1.
if u know then plz send me the code .
thanx in advance
Member
182 Points
2336 Posts
Microsoft
Re: ASP.NET 2.0 - Enter Key - Default Submit Button
Aug 07, 2006 10:01 AM|ranganh|LINK
>> Indeed, there's no need to do all this work.
The main aim of the defaultButton is to ensure Enter Key Submissions. Typically people are very much used to Enter Key submission and if we disable it might be annoying certain times.
The defaultButton really provides a way to handle Enter Keys for respective portions of the page when the focus is there.
Hope this clarifies.
Thanks.
Harish
http://geekswithblogs.net/ranganh
Contributor
2292 Points
1920 Posts
Re: ASP.NET 2.0 - Enter Key - Default Submit Button
Aug 07, 2006 10:36 AM|LudovicoVan|LINK
> The main aim of the defaultButton is to ensure Enter Key Submissions. Typically people are very much used to Enter Key submission and if we disable it might be annoying certain times.
Sorry, but i don't think so. A form submitted with an enter key is on the contrary a problem you encounter any time you have a *single* textbox in a form, and it is a legacy behaviour you usually have to... disable. Now the Fwk has a defaultButton property, but that outputs some javascript to handle key events and act accordingly, so this is something *more* than simply dealing with this legacy behaviour with enter keys. That is, i would at least combine them both: first, disable legacy behaviour, then eventually add support for enter key submission...
As [hondav] is telling:
> There is a BUG, to use Microsoft's own words and instead of fixing it they give you the option of deciding which way the bug is going to work.
So, great to have a defaultButton and great you have posted a sample usage. Yet i believe this should be unrelated to the legacy behaviour we are talking about.
My humble opinion. -LV
Member
10 Points
152 Posts
Re: ASP.NET 2.0 - Enter Key - Default Submit Button
Aug 14, 2006 02:29 PM|FMastro|LINK
I have this same question. Even worse is I have an asp:login which iselt creates the button. So how can I set the ASP.Net 2.0 Login window to fire the button click on Enter? And it's in a asp:content
FredMastro.Com
Member
33 Points
260 Posts
Re: ASP.NET 2.0 - Enter Key - Default Submit Button
Aug 21, 2006 06:10 PM|eappell|LINK
I've been struggling with how to capture the ENTER key and send the user one or the other result popup. Then someone directed me to your article here. I inserted two panels enclosing the two different sets of controls into the panels. I set the defaultbutton for the panels and tried it out. It works the first time I hit the ENTER key on any field. But if the user hits ENTER again, the first form is ALWAYS submitted. So, for example, if they didn't get the restuls they expected and went back to the main form and added more criteria and hit ENTER, it would submit the first form, which is incorrect. The user has to refresh the page to get the second form to submit on ENTER (or click the submit button). This won't work for us. I'm wondering if this is a bug, and if so is there a workaround? if not, what could I be missing to cause this behavior?
Thanks so much for your help - this webapp is nearly done, but this issue is causing me a lot of headaches! Hopefully this will be resolved soon.
Thanks again,
eddie
None
0 Points
1 Post
Re: ASP.NET 2.0 - Enter Key - Default Submit Button
Aug 23, 2006 01:03 PM|fengkui|LINK
None
0 Points
172 Posts
Re: ASP.NET 2.0 - Enter Key - Default Submit Button
Aug 23, 2006 09:09 PM|Yovav|LINK
I'm having the same problem with two controls on the same page (Signin & Signup)
I wonder if this was forgotten when creating ASP.NET coz I can not thing on any "EZ workaround" (as it was so EZ 2 implement on classic ASP)
any ideas ?
code samples ?
(hate to mess with all those low level events :-)
Contributor
2292 Points
1920 Posts
Re: ASP.NET 2.0 - Enter Key - Default Submit Button
Aug 24, 2006 06:47 AM|LudovicoVan|LINK
Yovav:> I'm having the same problem with two controls on the same page
Just an idea, not checked:
DefaultButton='<%= MyControl.FindControl("MyControlButton").ClientID %>'
Let me know.
-LV
None
0 Points
172 Posts
Re: ASP.NET 2.0 - Enter Key - Default Submit Button
Aug 24, 2006 12:49 PM|Yovav|LINK
Another sample code (to be applied on each control):
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="Signin.ascx.cs" Inherits="Signin" %>
<
asp:Panel DefaultButton="_buttonSignin" runat="server">...
</
asp:Panel><%@ Control Language="C#" AutoEventWireup="true" CodeFile="Signup.ascx.cs" Inherits="Signup" %>
<
asp:Panel DefaultButton="_buttonSignup" runat="server">...
</
asp:Panel>So now - whenever the control area is being clicked (or focused) or any control inside it - the default button will used,
It will not go to a default button if the focus is outside of these two controls (for example - on the web site header) - the following script might also B useful:
<script language="javascript">
// <!--
function onKeySignin()
{
if (window.event.keyCode == 13)
{
document.getElementById('<%=_buttonSignin.ClientID%>').focus();
document.getElementById('<%=_buttonSignin.ClientID%>').click();
}
}
document.attachEvent("onkeydown", onKeySignin);
// -->
</script>
None
0 Points
3 Posts
Re: ASP.NET 2.0 - Enter Key - Default Submit Button
Aug 28, 2006 01:09 AM|GrantDG|LINK
After much seraching, I found a workable solution to this problem. Basically we need to manually set up the same client code that would be injected if we were using a Default button for a panel.
Assume we have a <asp:Login ... /> control on a page named "Login1"
In the Page_Load event handler:
------
Control ctl = Login1.FindControl("LoginButton");
if(ctl != null && ctl is IButtonControl)
Login1.Attributes.Add("onkeypress", string.Format("javascript:return WebForm_FireDefaultButton(event, '{0}')",ctl.ClientID));
-------
I'm assuming that we also have the WebForm_FireDefaultButton client script file available on the page... (if not - you'll get a JavaScript error)
- Schultz
None
0 Points
58 Posts
Re: ASP.NET 2.0 - Enter Key - Default Submit Button
Sep 01, 2006 12:31 PM|rmr.adduri|LINK
Hi, In my site also this is the problem. in my site i am using master pages and in the master page i have a usercontrol in the usercontrol i have an image button. and when i go to login page in that login page i used a login control. when i open login page and enter the user name and password and hit enter then the search button gets fired. so i searched for this in the forms but i didint find any information regarding this. so can you please help me regarding this.
Ram Mohan Rao Adduri,
Techinical Associate,
Prokarma
Hyderabad.
Mobile: 9490118545
Member
116 Points
1251 Posts
Re: ASP.NET 2.0 - Enter Key - Default Submit Button
Sep 19, 2006 05:20 PM|dba123|LINK
Folks, you have to remember that not everyone is using old login controls. We are using the new .NET 2.0 Membership API. Thus .NET provides the button and textboxes. Plus, if you're using master pages, you can't simply play around with defaultbutton because what button are you referring to if you are using a master page? that is not possible as differrent detail pages will have it's own button!
This is stupid, there must be an easy workaround.
None
0 Points
58 Posts
Re: ASP.NET 2.0 - Enter Key - Default Submit Button
Sep 20, 2006 02:00 AM|rmr.adduri|LINK
Hi GrantDG,
Can you provide the WebForm_FireDefaultButton client script.
Ram Mohan Rao Adduri,
Techinical Associate,
Prokarma
Hyderabad.
Mobile: 9490118545
None
0 Points
58 Posts
Re: ASP.NET 2.0 - Enter Key - Default Submit Button
Sep 20, 2006 02:02 AM|rmr.adduri|LINK
Hi dba123,
If u find any solution for this, can u please suggest me to solve this problem.
Ram Mohan Rao Adduri,
Techinical Associate,
Prokarma
Hyderabad.
Mobile: 9490118545
Member
10 Points
13 Posts
Re: ASP.NET 2.0 - Enter Key - Default Submit Button
Sep 20, 2006 12:05 PM|netmongamma|LINK
This is something you should be able to do. I had a boss who insisted on it, rather than just disabling the enter key. It should be textbox driven though, its not a good idea to use Panels just for this purpose, unless you use them anyway for other reasons. So this can be used at the FORM level and PANEL level. Where else? Textbox (or other control) would be ideal.
<asp:TextBox defaultbutton="soandso">
You may have a reason to point one textbox to one button and another textbox to another button.
None
0 Points
58 Posts
Re: ASP.NET 2.0 - Enter Key - Default Submit Button
Sep 25, 2006 01:40 AM|rmr.adduri|LINK
Ram Mohan Rao Adduri,
Techinical Associate,
Prokarma
Hyderabad.
Mobile: 9490118545
None
0 Points
5 Posts
Re: ASP.NET 2.0 - Enter Key - Default Submit Button
Sep 25, 2006 03:17 PM|manish k|LINK
hi,
the above code given, is much complecated one.
Member
181 Points
81 Posts
Re: ASP.NET 2.0 - Enter Key - Default Submit Button
Oct 07, 2006 10:44 PM|bachbouch|LINK
For those who are using the .Net 2.0 login control, I just thought about a hacky way to implement the scenario where users can hit the Enter key to fire the login button event, here is how it works:
1- wrap your login control into a Panel and call it Panel1.
2- inset a button on stage and call it dummyButton, set it's width and height to 0, set it border to none and it is background anad foreground to white set it is text to empty string.( this is way, dummybutton will be invisible). If you are wondering why wouldn't I just set the visibility to false, well the answer is that it won't work.
3- set the defaultbutton of Panel1 to dummyButton and set put this code the dummyButton your click handler
bool crediteUser = Membership.ValidateUser(Login1.UserName, Login1.Password);
if (crediteUser)
{
FormsAuthentication.RedirectFromLoginPage(Login1.UserName,false);
}
Hope this helps
-----
Dont forget to click "Mark as Answer" on the post that helped you.
This credits that member, earns you a point and marks your thread as Resolved so we will all know you have been helped.
None
0 Points
8 Posts
Re: ASP.NET 2.0 - Enter Key - Default Submit Button
Oct 09, 2006 09:25 PM|medo75|LINK
Contributor
6294 Points
5754 Posts
ASPInsiders
Re: ASP.NET 2.0 - Enter Key - Default Submit Button
Oct 10, 2006 01:30 AM|StrongTypes|LINK
Member
10 Points
13 Posts
Re: ASP.NET 2.0 - Enter Key - Default Submit Button
Oct 10, 2006 01:31 AM|captobvious|LINK
None
0 Points
58 Posts
Re: ASP.NET 2.0 - Enter Key - Default Submit Button
Oct 10, 2006 03:19 AM|rmr.adduri|LINK
Ram Mohan Rao Adduri,
Techinical Associate,
Prokarma
Hyderabad.
Mobile: 9490118545
Contributor
6294 Points
5754 Posts
ASPInsiders
Re: ASP.NET 2.0 - Enter Key - Default Submit Button
Oct 10, 2006 08:10 AM|StrongTypes|LINK
Member
10 Points
152 Posts
Re: ASP.NET 2.0 - Enter Key - Default Submit Button
Oct 11, 2006 08:14 AM|FMastro|LINK
After much seraching, I found a workable solution to this problem. Basically we need to manually set up the same client code that would be injected if we were using a Default button for a panel.
Assume we have a <asp:Login ... /> control on a page named "Login1"
In the Page_Load event handler:
------
Control ctl = Login1.FindControl("LoginButton");
if(ctl != null && ctl is IButtonControl)
Login1.Attributes.Add("onkeypress", string.Format("javascript:return WebForm_FireDefaultButton(event, '{0}')",ctl.ClientID));
-------
I'm assuming that we also have the WebForm_FireDefaultButton client script file available on the page... (if not - you'll get a JavaScript error)
Not sure why you guys are still having problems after this post. I'm using .Net 2.0 Login controls and this worked great for me. I'm using VB thought I coudln't translate the If Then check to see if it's a button and null, but the rest of it worked great. What's the reason for all this Panel1 and invisible buttons for?
My Code Behind, just like the post:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim ctl As Control = userLogin.FindControl("LoginButton")
userLogin.Attributes.Add("onkeypress", String.Format("javascript:return WebForm_FireDefaultButton(event, '{0}')", ctl.ClientID))
End Sub
My ASPX Page:
<asp:Login ID="userLogin" runat="server" LoginButtonImageUrl="~/images/buttons/log_in.gif"
LoginButtonType="Image" PasswordRecoveryText="Forgot Password?" PasswordRecoveryUrl="/ForgotPassword.aspx"
TitleText="User Login" UserNameLabelText="User ID:" >
<TitleTextStyle Font-Bold="True" Font-Size="Medium" ForeColor="SteelBlue" />
<TextBoxStyle Width="135px" />
<LabelStyle Font-Bold="True" />
</asp:Login>
FredMastro.Com
None
0 Points
5 Posts
Re: ASP.NET 2.0 - Enter Key - Default Submit Button
Oct 30, 2006 04:36 PM|sdgscott|LINK
Thanks FMastro,
I added a generic help button to my master page and the enter key started firing the help button instead of the login
button. Your solution is just what I needed!
None
0 Points
58 Posts
Re: ASP.NET 2.0 - Enter Key - Default Submit Button
Nov 06, 2006 01:00 AM|rmr.adduri|LINK
Hi,
How we enable login error message when we hit on the default button
Ram Mohan Rao Adduri,
Techinical Associate,
Prokarma
Hyderabad.
Mobile: 9490118545
Member
36 Points
191 Posts
Re: ASP.NET 2.0 - Enter Key - Default Submit Button
Nov 15, 2006 01:04 PM|jjamjatra|LINK
I chased this issue a lot and think I understand it now. I had a content page with 2 usercontrols. One contained a textbox for zipcode and a Find button. The 2nd user control contained a LoginView and Login control, the latter of which generates textboxes for username and password and a button for Login. There were numerous difficulties getting this to work but now the user can enter text in either box and hit Enter. The context is determined and the proper button is fired (without an explicit click of the desired button nor moving focus to the desired button). Here is the code in VB.Net:
Thanks to Imar at http://www.spaanjaars.com/QuickDocId.aspx?quickdocid=379 I now have a complete solution to this issue. In my LoginMechanism.ascx user control I have the following:
<div id="intelliTxt">Dim myLogin As Login = CType(Me.LoginView1.FindControl("Login1"
If myLogin IsNot Nothing Then
Dim myButton As Button = CType(myLogin.FindControl("LoginButton")
If myButton IsNot Nothing Then
Me.Page.Form.DefaultButton = myButton.UniqueID
Dim myFoundTextBox As TextBox = CType(myLogin.FindControl("Password"), TextBox)
myFoundTextBox.Attributes.Add("onclick", "document.forms[0].onkeypress = " _
+ "new Function(""return WebForm_FireDefaultButton(event, '" + myButton.UniqueID + "');"");")
End If
End If
And in my FindZipCodeNearYou.ascx user control I have the following:
Dim myButton As Button = CType(Me.FindControl("btnFind"), Button)
If myButton IsNot Nothing Then
Me.Page.Form.DefaultButton = myButton.UniqueID
Dim myFoundTextBox As TextBox = CType(Me.FindControl("txtZipCode"), TextBox)
myFoundTextBox.Attributes.Add("onclick", "document.forms[0].onkeypress = " _
+ "new Function(""return WebForm_FireDefaultButton(event, '" + myButton.UniqueID + "');"");")
End If
Both of these user controls are used on the same content page within a master page. Now in either textbox above, the user can type his password (or zipcode) and immediately hit the ENTER key and the proper button will be fired.</div>
defaultButton onkeypress firedefaultbutton enter key
None
0 Points
4 Posts
Re: ASP.NET 2.0 - Enter Key - Default Submit Button
Dec 19, 2006 02:58 PM|Ellipse|LINK
I wanted to mention that the Login control was the easiest to get working with some of the above solutions using the javascript:return WebForm_FireDefaultButton stuff or you can do this... (check for nulls if you want... I didn't include that here)
Page.Form.DefaultFocus = Login1.FindControl(
"UserName").UniqueID;Page.Form.DefaultButton = Login1.FindControl("LoginButton").UniqueID;
However my problem was figuring out how to reference textboxes and buttons in other controls like the Password Recovery Control and the Create User Wizard Control. Something like... PasswordRecovery1.FindControl("UserName").UniqueID will not return the username textbox like it does for the Login Control. The UserName control is hiding in some containers (which I can't seem to find the name from anywhere in VS... anyone know where this is?). I only found the name for the control by using View Source in my browser.
I was able to come up with this for the Password Recovery Control:
Page.Form.DefaultFocus = PasswordRecovery1.FindControl("UserNameContainerID$UserName").UniqueID;
Page.Form.DefaultButton = PasswordRecovery1.FindControl("UserNameContainerID$SubmitButton").UniqueID;
For Create User Wizard I used:
Page.Form.DefaultFocus = CreateUserWizard1.FindControl(
"CreateUserStepContainer$UserName").UniqueID;The default button seems to be already set on the Create User Wizard.
I'm mainly just posting this for people searching for the solution for the controls other than the Login because I spend hours yesterday trying to reference these controls until my brain clicked on and I decided to use View Source to get the references for the controls.
Goodluck!
Default Button Login Control Create User Wizard DefaultFocus Set Focus Password Recovery
Member
21 Points
108 Posts
Re: ASP.NET 2.0 - Enter Key - Default Submit Button
Dec 20, 2006 04:52 PM|AspForumTanya|LINK
Wow, I've been looking for the same answer. Worked for me as well. Had similar problem.
Thanks a lot!
Tatyana
None
0 Points
3 Posts
Re: ASP.NET 2.0 - Enter Key - Default Submit Button
Jun 27, 2007 07:13 AM|stevehoff|LINK
Tagging this for later. A bit hackish, but it certainly solved my problem when a usercontrol had a search box in a child contol as well as a login box.
— Alzheimer's Law of Programming
Member
409 Points
1231 Posts
Re: ASP.NET 2.0 - Enter Key - Default Submit Button
Jun 29, 2007 09:31 PM|rmdw|LINK
I just tried out what Ellipse suggested, namely:
Page.Form.DefaultFocus = Login1.FindControl("UserName").UniqueID;
Page.Form.DefaultButton = Login1.FindControl("LoginButton").UniqueID;
I placed this code in the Page_Load event handler. I even stepped through the code and it *seemed* to work correctly but when the page actually loaded, only the Default button was set but the cursor was definitely not in the UserName textbox.
Now, my login control is sitting inside of a couple of panels which are sitting in a content page (ie. connected to a master page). Might this have something to do with it?
I'm hoping someone else might have an idea why!
Robert W.
Vancouver, BC
Vancouver, BC
Technical Blog
Pocket Pollster
Member
409 Points
1231 Posts
Re: ASP.NET 2.0 - Enter Key - Default Submit Button
Jun 29, 2007 10:06 PM|rmdw|LINK
I just discovered that Ellipse's code is working. The "problem" is that my login control resides inside of a panel that has the Ajax Control Toolkit's RoundedCornersExtender improving its appearance. So though the cursor is being correctly focused, then when the Ajax control does its work, the focus is lost.
I'm going to place a question on a different forum to try to find an answer for this!
Robert
Vancouver, BC
Technical Blog
Pocket Pollster
Member
25 Points
307 Posts
Re: ASP.NET 2.0 - Enter Key - Default Submit Button
Jul 10, 2007 02:58 PM|xuanvu|LINK
I've used this code and it work for the first step (asking for Username):
Page.Form.DefaultFocus = PasswordRecovery1.FindControl("UserNameContainerID$UserName").UniqueID;
Page.Form.DefaultButton = PasswordRecovery1.FindControl("UserNameContainerID$SubmitButton").UniqueID;
But then the next step (asking to answer the secret question), if I hit the enter key after answered the question, it won't work... I have some textbox and another submit button on the page for other purposes. I've tried to add this:
Page.Form.DefaultButton = PasswordRecovery1.FindControl("QuestionContainerID$SubmitButton").UniqueID
on the Page load below Page.Form.DefaultButton = PasswordRecovery1.FindControl("UserNameContainerID$SubmitButton").UniqueID;
but this somehow make the first step (asking for username) not work (hit the Enter key).
Any idea?
Thanks,
Kenny.
Member
409 Points
1231 Posts
Re: ASP.NET 2.0 - Enter Key - Default Submit Button
Jul 10, 2007 04:12 PM|rmdw|LINK
Kenny,
I'm not 100% sure but I think your code may be correct but it's just located in the wrong place. Here's the code I implemented, which I still need to extensively test:
protected void Page_Load(object sender, EventArgs e)
{
// Make the Submit button the default button and place the cursor in the User Name textbox
if (!IsPostBack)
{
Page.Form.DefaultButton = PasswordRecovery1.FindControl("UserNameContainerID$SubmitButton").UniqueID;
Page.Form.DefaultFocus = PasswordRecovery1.FindControl("UserNameContainerID$UserName").UniqueID; // Doesn't currently work because of Ajax RoundedCornersExtender
}
}
// This is called after the User Name is entered; we'll use it to change the default controls
protected void PasswordRecovery1_VerifyingUser(object sender, LoginCancelEventArgs e)
{
Page.Form.DefaultButton = PasswordRecovery1.FindControl("QuestionContainerID$SubmitButton2").UniqueID;
Page.Form.DefaultFocus = PasswordRecovery1.FindControl("QuestionContainerID$Answer").UniqueID; // Doesn't currently work because of Ajax RoundedCornersExtender
}
The second event handler is being called because of this: <asp:PasswordRecovery ID="PasswordRecovery1" runat="server" OnVerifyingUser="PasswordRecovery1_VerifyingUser">
Hope this helps!
Robert
Vancouver, BC
Technical Blog
Pocket Pollster
Member
25 Points
307 Posts
Re: ASP.NET 2.0 - Enter Key - Default Submit Button
Jul 10, 2007 05:24 PM|xuanvu|LINK
Hi Robert, added OnVerifyingUser made it work.
Thanks,
Kenny.
None
0 Points
2 Posts
ASP.NET 2.0 - Enter Key - Default Submit Button
Jul 11, 2007 12:33 PM|Vishal K. Bongale|LINK
Thanks in advance…
Member
409 Points
1231 Posts
Re: ASP.NET 2.0 - Enter Key - Default Submit Button
Jul 11, 2007 04:41 PM|rmdw|LINK
Hi Vishal,
I'm not sure why it's not working as you feel it should.
Now, I don't use the ValidationSummary control (just don't like it) but take a look at this page: http://pocketpollster.com/beta
On the right side there are two RequiredFieldValidator controls and one RegularExpressionValidator control. Place the cursor inside either textbox and press Enter. You'll notice that the validators are fired.
Note: This Beta site isn't yet wired up with SQL Server so it's far from fully functional.
Vancouver, BC
Technical Blog
Pocket Pollster
None
0 Points
3 Posts
Re: ASP.NET 2.0 - Enter Key - Default Submit Button
Sep 13, 2007 06:35 PM|altodi|LINK
Hi guys,
I've used this code that worked for me
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
Me.Form.DefaultButton = PasswordRecovery1.UserNameTemplateContainer.FindControl("SubmitButton").UniqueID
CType(PasswordRecovery1.UserNameTemplateContainer.FindControl("UserName"), TextBox).Focus()
End If
End Sub
Protected Sub PasswordRecovery1_VerifyingUser(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.LoginCancelEventArgs) Handles PasswordRecovery1.VerifyingUser
Me.Form.DefaultButton = PasswordRecovery1.QuestionTemplateContainer.FindControl("SubmitButton").UniqueID
CType(PasswordRecovery1.QuestionTemplateContainer.FindControl("Answer"), TextBox).Focus()
End Sub
That did the work
None
0 Points
27 Posts
Re: ASP.NET 2.0 - Enter Key - Default Submit Button
Sep 17, 2007 08:43 AM|rwc_chris|LINK
Thanks, this was helpful!
Member
1 Points
77 Posts
Re: ASP.NET 2.0 - Enter Key - Default Submit Button
Dec 12, 2007 08:05 PM|Laplain|LINK
Thanks. I've been looking for a way to convert from v.1 to v.2 from the MetaBuilders WebControl. Using the Panel "DefaultButton" works great for me.
None
0 Points
1 Post
Re: ASP.NET 2.0 - Enter Key - Default Submit Button
Jan 04, 2008 06:34 AM|coolbear_lq@hotmail.com|LINK
I think I have a even better solution for the Default Enter Key in Login Control. You don't need to write any code. Just follow the following steps:
1. Convert the Login control into a template by selecting the control and clicking the the arrow button on the top right corner and select "Convert to Template".
2. In your Login control template design view, add a Panel into the template and put everything in the Login control into the Panel.
3. When you convert your Login control to a template, the submit button in the Login control is now separated out. Select on the submit button to find out its name and fill the name into the DefaultButton property of the Panel inside the Login control template.
Problem solved.
None
0 Points
1 Post
Re: ASP.NET 2.0 - Enter Key - Default Submit Button
Jan 16, 2008 05:48 AM|parcol|LINK
This does the job with indead minimal coding.
To set the focus to the username textbox I added the following code to the Page_Load:
TextBox userName = (TextBox)Login1.FindControl("UserName");
userName.Focus();
This works for me.
None
0 Points
3 Posts
Re: ASP.NET 2.0 - Enter Key - Default Submit Button
Jan 22, 2008 01:31 AM|India.net|LINK
hi everyone , I am also having the same problem and tried all the solutions but they r working only for single text box.if i set defaultbutton property of a form then it works for first text box only. also i have tried other solutions ,plz suggest some solution.[Idea]
Member
409 Points
1231 Posts
Re: ASP.NET 2.0 - Enter Key - Default Submit Button
Jan 22, 2008 02:47 AM|rmdw|LINK
I don't understand what you're trying to say. An example would help as a starting point.
I invite you to visit this page: http://pocketpollster.com/beta/Feedback/infoRequest.aspx It works properly for my needs. How close is yours to it?
Robert
Vancouver, BC
Technical Blog
Pocket Pollster
Member
520 Points
109 Posts
Re: ASP.NET 2.0 - Enter Key - Default Submit Button
Jan 22, 2008 05:18 AM|zirmandli|LINK
Hi
to make button to default button for the page, try this
Page.Form.DefaultButton = btnSubmit.UniqueID;
-- Benazir Mandli
Participant
893 Points
295 Posts
Re: ASP.NET 2.0 - Enter Key - Default Submit Button
Feb 18, 2008 11:31 AM|ezhillmaran|LINK
I to face this problem in program. but i'm using master page. when i open another webform and enter the hit button it executes the master page button , but it supposed to execute current webform button
Maran.
Member
12 Points
95 Posts
Re: ASP.NET 2.0 - Enter Key - Default Submit Button
Mar 26, 2008 05:05 AM|gnuts4lunch|LINK
i had the same problem and tried the code you suggested...i got the error below;
Error 1 The name 'Login1' does not exist in the current context C:\inetpub\wwwroot\wbsa\index.aspx.cs 18 35 http://localhost/wbsa/
Member
12 Points
95 Posts
Re: ASP.NET 2.0 - Enter Key - Default Submit Button
Mar 26, 2008 07:09 AM|gnuts4lunch|LINK
Hi Parcol,
Can you show me some code on your panel workaround?
I tried your suggestions and it gave me some errors.
Thanks
Member
135 Points
75 Posts
Re: ASP.NET 2.0 - Enter Key - Default Submit Button
Apr 24, 2008 03:27 AM|zubinjoshi|LINK
how this can be solved in .net 1.1
suppose there are two frame one for new login and another for existing login ........ with 2 textboxes and one button in each frame...
when user mouse is in the perticular textbox the corresponding button should fire ,,, as that we have in .net 2.0 through defaultbutton in div
how can be done in .net 1.1
Participant
893 Points
295 Posts
Re: ASP.NET 2.0 - Enter Key - Default Submit Button
Apr 24, 2008 03:38 AM|ezhillmaran|LINK
Set default button as new login for frame1 and existing login for frame 2
Maran.
Contributor
4954 Points
1726 Posts
Re: ASP.NET 2.0 - Enter Key - Default Submit Button
Apr 24, 2008 06:17 AM|thirumaran007|LINK
Hi Friend,
Just use access key property to do that
With Friendly,
Thirumaran
Participant
1341 Points
352 Posts
Re: ASP.NET 2.0 - Enter Key - Default Submit Button
Apr 28, 2008 08:02 AM|ShivaKarthik|LINK
No Need Of this much of stuff.Just u can do in the following way
//the button which you want as default button define it in defaultbutton
<
form id="form1" runat="server" defaultbutton= "btn_Submit">Mumbai , India
-------------------------------------------------
Don't Forget to Mark as Answer for the post that helped you.
-------------------------------------------------
None
0 Points
1 Post
Re: ASP.NET 2.0 - Enter Key - Default Submit Button
May 13, 2008 08:03 AM|Zarna|LINK
Page.Form.DefaultButton = Me.FindControl("Button1").UniqueID
it works successfully .
Write it on Page_Load Event
Member
16 Points
133 Posts
Re: ASP.NET 2.0 - Enter Key - Default Submit Button
May 14, 2008 09:41 AM|stapes|LINK
Member
409 Points
1231 Posts
Re: ASP.NET 2.0 - Enter Key - Default Submit Button
May 14, 2008 12:23 PM|rmdw|LINK
The ideas presented already are good ones. Here's another, more extensive example that definitely works:
<div id="1f2i" class="ArwC7c ckChnd"> <tr><asp:Panel ID="newsletterPanel" CssClass="newsletter" runat="server">
<table style="background-color:#B9C8FF; border-style:none; width:100%">
<td class="smallText" colspan="2">
Sign up for our newsletter:
</td>
</tr>
<tr>
<td class="smallText">Email:</td>
<td class="rightTextBox">
<asp:TextBox ID="textBoxSubscribeEmail" CssClass="smallTextBox" runat="server" ValidationGroup="Newsletter" />
</td>
</tr>
<tr>
<td class="smallText">Name:</td>
<td class="rightTextBox"><asp
</tr>
<tr>
<td align="right" colspan="2">
<asp:Button ID="buttonSubscribe" CssClass="smallTextButton" runat="server" Text="Subscribe" OnClick="buttonSubscribe_Click" ValidationGroup="Newsletter" />
<br />
<div class="smallText" style="text-align:left; padding-top:5px">
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
/>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
/>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1
/>
</div>
</td>
</tr>
</table>
</asp:Panel>
<cc1:RoundedCornersExtender ID="RoundedCornersExtender1" runat="server" TargetControlID="newsletterPane
</ContentTemplate>
</asp:UpdatePanel>
// Pressed when the user wants to subscribe to the PP newsletter
protected void buttonSubscribe_Click(object sender, EventArgs e)
{
string retMsg = DBAccess.AddSubscriber(textBoxS
ShowMessage(retMsg);
textBoxSubscribeEmail.Text = "";
textBoxSubscribeName.Text = "";
}
protected void Page_Load(object sender, EventArgs e)
{
// Make the Newsletter Subscribe button the default button
if (!IsPostBack)
{
Page.Form.DefaultButton = buttonSubscribe.UniqueID;
}
}</div>
Vancouver, BC
Technical Blog
Pocket Pollster
Member
16 Points
133 Posts
Re: ASP.NET 2.0 - Enter Key - Default Submit Button
May 14, 2008 12:30 PM|stapes|LINK
rmdw - Could you explain that code?
Member
409 Points
1231 Posts
Re: ASP.NET 2.0 - Enter Key - Default Submit Button
May 14, 2008 01:35 PM|rmdw|LINK
That's a very vague question. What precisely do you want to know?
The key line is this one in the Page_Load event handler: Page.Form.DefaultButton = buttonSubscribe.UniqueID;
Robert
Vancouver, BC
Technical Blog
Pocket Pollster
Member
9 Points
327 Posts
Re: ASP.NET 2.0 - Enter Key - Default Submit Button
Sep 25, 2008 02:02 PM|liju.thn|LINK
hi all i have got a fine piece of code for the master - content page issue of default submit buttons .put this in page load of master page
txtpwd.Attributes.Add("onkeydown", "if(event.which || event.keyCode){if ((event.which == 13) || (event.keyCode == 13)) {document.getElementById('"+btnsubmit.UniqueID+"').click();return false;}} else {return true}; ");
None
0 Points
4 Posts
Re: ASP.NET 2.0 - Enter Key - Default Submit Button
Nov 04, 2008 02:13 PM|SigHandel|LINK
Member
1 Points
32 Posts
Re: ASP.NET 2.0 - Enter Key - Default Submit Button
Nov 19, 2008 04:21 AM|hemens|LINK
Hii,
I tried the above in form tag of my default page, but it gives error stating " form1 button id should be of I BUTTON CONTROL.
I am calling user control in home page, so my form tag is in home page and defaultbutton thing is not working as not able to find i gues...
how do i resolve...
....hemens
Member
409 Points
1231 Posts
Re: ASP.NET 2.0 - Enter Key - Default Submit Button
Nov 19, 2008 04:06 PM|rmdw|LINK
Hi Everyone,
It has been a while since I wrote that code. I just tested it and it works fine. Here's proof: http://pocketpollster.com/beta
Looking at it now, the pertinent code only appears to be this:
<asp:Button ID="buttonSubscribe" CssClass="smallTextButton" runat="server" Text="Subscribe" OnClick="buttonSubscribe_Click" ValidationGroup="Newsletter" />
And:
protected void Page_Load(object sender, EventArgs e)
{
// Make the Newsletter Subscribe button the default button
if (!IsPostBack)
{
Page.Form.DefaultButton = buttonSubscribe.UniqueID;
}
}
I'm by no means the expert in this area, but what I've written does work for me. Hopefully it'll help you.
Robert
Vancouver, BC
Technical Blog
Pocket Pollster
Member
10 Points
42 Posts
Re: ASP.NET 2.0 - Enter Key - Default Submit Button
Dec 09, 2008 06:43 AM|codemobile|LINK
WHat in case of multiple default buttons ? Can i use multiple ?
Participant
1532 Points
394 Posts
Re: ASP.NET 2.0 - Enter Key - Default Submit Button
Jan 16, 2009 02:11 PM|Pradeep Kr. Sharma|LINK
Hi,
Any one create more then one submit button in the application. Here is the Example. That create more then one submit button depending on the focus of the text button before hit enter.
Call makeSubmitButton on the page_Load and pass the textBoxId and also ButtonId which you want to make submit button when focus is on txtBoxId.
It will set ButtonId as a default button....
public void makeSubmitButton(System.Web.UI.WebControls.TextBox txtBox, System.Web.UI.WebControls.LinkButton CmdButton)
{
txtBox.Attributes.Add("onkeydown", "if(event.which || event.keyCode){if ((event.which == 13) || (event.keyCode == 13)) {document.getElementById('" + CmdButton.UniqueID + "').click();return false;}} else {return true}; ");
}
Sincerely,
Pradeep Kr. Sharma
Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Pradeep Kr. Sharma
Please remember to "Mark as Answer" the responses that resolved your issue.
Member
30 Points
32 Posts
Re: ASP.NET 2.0 - Enter Key - Default Submit Button
Jan 17, 2009 02:47 AM|Shailendra Sankhala|LINK
following code is for default button.
<form id="form1" runat="server" defaultbutton="btnlogin" >
--
--
</form>
None
0 Points
1 Post
Re: ASP.NET 2.0 - Enter Key - Default Submit Button
Jan 26, 2009 01:34 AM|Vitesh Tandel|LINK
Member
2 Points
16 Posts
Re: ASP.NET 2.0 - Enter Key - Default Submit Button
Jan 27, 2009 12:13 PM|Nicoxx|LINK
thanks for the article... it worked for me :)
<form id="form1" runat="server" defaultbutton="btn1">
usefull part
Contributor
4387 Points
1407 Posts
Re: ASP.NET 2.0 - Enter Key - Default Submit Button
Mar 01, 2009 01:00 AM|anzer|LINK
It will simply postback rather than going to next line :( . And you can not ignore Firefox as now it have almost half of the browser share
http://www.w3schools.com/browsers/browsers_stats.asp
ClientSideAsp.Net | Blog
None
0 Points
1 Post
Re: ASP.NET 2.0 - Enter Key - Default Submit Button
Apr 03, 2009 03:59 AM|delve|LINK
i master page and 3 webcontrol
1. for loging
2. for search with site
3 for google search
i have used your code work gr in ie7.
But this is not woking in firefox (version 3.0.8) as our site user share is more then 28 % so i cannot ignore that can u help.
i tried almost all the technique but not desire result. every time is goes to login button in firefox.
None
0 Points
2 Posts
Re: ASP.NET 2.0 - Enter Key - Default Submit Button
Apr 11, 2009 08:46 AM|prabhu_17|LINK
Actually , tell me actually .. what kind of code u wants
Member
11 Points
25 Posts
Re: ASP.NET 2.0 - Enter Key - Default Submit Button
Apr 16, 2009 10:42 AM|ILoveFall|LINK
In page load add attributes for your control and you can use keycode on html page in javascript:
event.keyCode == 13 //For enter button
None
0 Points
6 Posts
Re: ASP.NET 2.0 - Enter Key - Default Submit Button
Apr 22, 2009 02:27 AM|chope_07|LINK
Try using this, Assuming that we have asp TextBox with an ID of "TextBox1" and Button with an ID of "Button1" controls on our page.
Put this code on the Head of your aspx page.
<script language="javascript">
function doClick(e, buttonid){
var evt = e ? e : window.event;
var bt = document.getElementById(buttonid);
if (bt){
if (evt.keyCode == 13){
bt.click();
return false;
}
}
}
</script>
And this on page load event.
TextBox1.Attributes.Add("onkeypress", "return doClick(event,'" + Button1.ClientID + "')");
Default Button
http://www.stringlist.com
Member
1 Points
77 Posts
Re: ASP.NET 2.0 - Enter Key - Default Submit Button
May 06, 2009 02:38 PM|Laplain|LINK
Using a Panel's "DefaultButton" worked perfectly for my needs. I have three text box functions on one page, and just made sure each "tool" had a Panel surrounding it, then added the button as the Default to each panel. Thanks! [Yes]
Default Button panel
Member
540 Points
188 Posts
Re: ASP.NET 2.0 - Enter Key - Default Submit Button
Nov 04, 2009 02:36 AM|Babunareshnarra|LINK
Check these out
http://stackoverflow.com/questions/333141/asp-net-default-button
http://www.velocityreviews.com/forums/t121858-how-do-i-use-the-defaultbutton-setting-when-using-master-pages.html
Hope this helps.
Babu Naresh Narra
Remember to click “Mark as Answer” on the post If you get answer from my post(s) !
Member
110 Points
24 Posts
Re: ASP.NET 2.0 - Enter Key - Default Submit Button
Jan 27, 2010 01:21 PM|abaidurrehman|LINK
This is happen cause of WebResource.axd. if WebResource.axd get corrupted asp:panel will not work
http://webdevelopmentgeeks.blogspot.com/
None
0 Points
1 Post
Re: ASP.NET 2.0 - Enter Key - Default Submit Button
Aug 01, 2011 11:50 AM|way_prasad|LINK
Hi can you please give the solution with javascript disabled ?
None
0 Points
1 Post
Re: ASP.NET 2.0 - Enter Key - Default Submit Button
Oct 08, 2011 10:40 AM|jakenet|LINK
Hi everyone
not sure if I've missed something with the problem, but I have a textbox on my form and everytime I hit enter, the form would accept the changes to the yexybox and process the order.
My solution was to include a 'fake' button that did nothing, something like this
<form id="form1" runat="server" defaultbutton="btnfake">
<asp:Button ID="btnfake" runat="server" Width="0px" Height="0px" BorderColor="White" BackColor="White" text="" ForeColor="White" BorderWidth="0px"/>
I set visible=false at first but that didn't seam to work, so setting everything to the same colour as the background (i.e. white in my case) and that did the trick.
Hope that all makes sense.
Mick