Handle Enter Key on Website (ASP and VB) - asp.net

So I have a website with multiple asp controls. When I press enter inside by login form, the search function runs because it's the first thing found on the page.
How would I handle the enter button so that when the active textbox is for the login form, the loginbutton code actually runs rather than the searchbutton.
One last problem is that the login controls are inside a loginview so the hierarchy shows that the asp:textbox and asp:button for logging in are inside 3 tags like so:
<loginview>
<login>
<logintemplate>
//controls are here.
</logintemplate>
</login>
Just a note that all controls are asp and that all code is prefered in VB. Thanks

You want to set the default button on the form like this:
Page.Form.DefaultButton = "LoginButtonId"
MSDN Docs: http://msdn.microsoft.com/en-us/library/system.web.ui.htmlcontrols.htmlform.defaultbutton%28VS.80%29.aspx
Also, see this question for an issue I had in Firefox related to this : Default Form Button in FireFox

Related

UserControl in AJAX ModalPopupExtender in another UserControl

I am working on a ASP.NET (with Visual Basic) web application.
I have been trying to make a user control for two days, but i have an issue that i could not solve.
I have a FormView that I want to fill with an object, that i get from another user control. This second user control must be shown in a ModalPopupExtender when I click on an ImageButton. This works correctly. However, when i use the control, the modal popup is closed.
I use the same user control in a ModalPopupExtender, in others .aspx pages, and it works correctly, so I don't think that the error comes from it. It fails only when the user control and ModalPopupExtender are used in another control.
I show you some screenshots to explain easier :
www.noelshack.com/2012-39-1348649045-1.png
1 : .aspx web page,
2 : Button which shows the popup,
3 : .ascx user control,
4 : Button which closes the popup
This configuration works correctly : I can use my user control and the popup is closed only when it must be.
www.noelshack.com/2012-39-1348649045-2.png
1 : .ascx user control,
2 : ImageButton which shows the popup,
3 : .ascx user control,
4 : Button which closes the popup
The popup with the control is correctly opened, but when i try to search with the control (click on "RECHERCHER"), the popup is closed.
www.noelshack.com/2012-39-1348649045-3.png
Then, if I click again on the ImageButton that shows the popup
www.noelshack.com/2012-39-1348649045-4.png
We can see that the user control worked correctly.
So the only problem is that the popup is closed when I try to use the user control.
And this occurs only when i work in another user control, it works correctly in a page.
Here is the part of my code where i use the popup and the user control :
In the page, which works :
www.noelshack.com/2012-39-1348649045-6.png
In the user control, which does not work :
www.noelshack.com/2012-39-1348649044-5.png
You can ask me if you don't understand something or if you need more explanations
Thank you for your help and sorry for my english. Sorry for the links, I can't post images or hyperlinks...
Try using the following at the bottom of the RECHERCHER button:
mymodalpopupextender.show()

post asp.net form

I have an aspx page. In tag i wrote action property to some url.
There is a submit button, when i press it the page is submitted. This is quite simple....
But what i will do when i have to submit that page to three different URLs upon pressing three different submit buttons? How would i handle the action property of tag for three different URLs. Can i submit form from server side?(I mean by changing action property of form dynamically).
I am new to asp.net please help me as soon as possible.
I will be thankful to you...
take a look at the PostBackUrl-Property as explained here:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.postbackurl.aspx

ASP.NET 4 Password Recovery Validation failing in IE only

I'm using the ASP.NET Membership provider and using the Password Recovery control to reset the user password if they forget it. On the whole it all works fine, but with one catch when it comes to validation.
I have expanded the PasswordRecovery control out to use the template feature to customise the appearance, which is all fine. I have set the user lookup error handler using:
OnUserLookupError="PasswordRecovery1_UserLookupError"
in the opening tag and if I load up the page, type in some junk name and click submit this fires as expected, in all browsers. The snippet of VB code makes an error div visible and sets some text.
However, if upon loading the page for the first time I type a duff name into the username box and hit the enter key to submit the form, in Internet Explorer (version 8), the UserLookupError event fails to trigger. It triggers just fine in Chrome or Firefox, just not in IE. I know it's not a focus issue on the form, as I can see the form is being submitted.
If I click the submit button first, then following that hit the enter key it does fire, it's just that first time it doesn't, and only in IE.
Also to add that if I set breakpoints in the VB code to check to see if the page is being submitted, I can see the Page_Load event fire when I hit the enter key, but not the sub PasswordRecovery1_UserLookupError.
Has anyone else seen this? It looks like a bug in IE (no really???), but I need to nail it down.
Any help would be welcome.
OK, after some more digging and testing this is actually only a default button issue. I'm not sure what's happening when the enter key is being pressed (only in IE) after text has been entered in the box, as it is reloading the page but not submitting it. There is no other control or function on the page which would navigate back to or refresh this page.
Despite the submit button appearing to be the default button it actually isn't. The key here therefore is setting the defaultbutton property of the webform. That however, when the button is inside a PasswordRecovery control is another issue.
EDIT:
Here's the fix. Set the default button at the page load stage.
Me.Form.DefaultButton = PasswordRecovery1.UserNameTemplateContainer.FindControl("SubmitButton").UniqueID

textbox - KeyPress event on Asp.net [no javascript]

I'm designing a website and I want my login page to catch "Enter" with all components, but i can not assign any Key or Mouse Events to TextBox ... After pressing to "Enter" a function should be run with 2 parameters (user name and password).
How can i do this?
If you wrap all the controls inside a standard HTML form then hitting enter in one of the text boxes will submit the form. You can then process the username and password entered on the server as with a normal form submission. Is that the behaviour you're looking for?
You don't need to trap any events on the client in this case so no need for JavaScript.
How to submit ASP.NET forms with the enter key
Read up on the defaultbutton property for forms and panels. This should get you what you want. From the site above:
ASP.NET 2.0 introduces a wonderful work around for this. By simply specifying the "defaultbutton" property to the ID of the , 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 definition tag. The form level setting is overridden when specified at the panel level, for those controls that are inside the panel.
http://www.beansoftware.com/asp.net-tutorials/accept-enter-key.aspx
finally if found the answer. Here is ! The only problem is, I can not hide my button,
so that i put that button to web page as "Login" ... Defaultbutton could be hidden or not?

ASP.NET default button

I have a master page with a search box and button at the top. This search functionality is taking over the "enter" key for all my web forms that use this master page. That is, if I have a login page that uses this master page and the user enters in their username/password and hits "enter", instead of logging in the user the system performs a search.
What's the best way to set up the default submit buttons. I could wrap everything in asp Panels and set the DefaultButton property, but that seems a bit tedious. Is there a better way?
use defaultbutton property of form or panel
<form defaultbutton=“button1” runat=“server”>
<asp:button id=“button1” text=“Same Page” runat=“server”/>
<asp:panel defaultbutton=“button2” runat=“server”>
<asp:textbox id=“foo” runat=“server”/>
<asp:button id=“button2” runat=“server”/>
</asp:panel>
</form>
As you have discovered default buttons can cause issues when there is more than one button on a page. I would take the Geeks suggestion but simplify it by removing the setfocus client script and extend it by adding the keydown event to both the search textbox and the login textboxes such that the enter key fires the correct button depending on if your user is using the search box or the log in box, or any other textbox you want to add the javascript to.
You can set focus on loading the page (in code behind if you like) to save the user some mouse work or tabbing if there is a sensible control for the user to start at, but otherwise the control the user is interacting with should determine the flow of the page and what the enter key does.
set focus on the text box ,
Page.RegisterStartupScript("SetFocus", "< script >document.getElementById('" + TextBox1.ClientID + "').focus();< /script >");
and then
In the keydown event for the last textbox you can do something like this:
If e.KeyCode = Keys.Enter Then
Me.Button1.Select()
End If
Rather than using javascript to manipulate the page you could put the search box and the submit button into an IFRAME on the master page. If the focus is in the iframe clicking will submit the search form, if the user is on your main form within the page they will submit the normal page.
The <iframe> src attribute points to a little self contained aspx page holding your text box and submit button which redirects to your search results form.
This is what we tried on our project which seemed to work. I changed the Search asp:Button to be an asp:LinkButton. I then added some CSS style to give it a background image to make it look like a button. LinkButtons are apparently not used by the page for determining which Button is the default action when pressing Enter. The sweet part was that LinkButton can still have click actions associated with them so I didn't have to change any of my code-behind stuff.

Resources