I must have read a hundred forum and blogposts about this, all talking about placing a Panel and setting the Default button to the Login button, or setting the Default button in the Form. I've tried all this plus lots more, but nothing works.
I have two LinkButtons in my Login control, which is converted to a template. The first is the Login button, the second is a link to a register page. When I hit enter, the Register LinkButton fires and redirects to the register page.
I've tried setting the default button on panels and placing them everywhere and setting the default butt开发者_JAVA技巧on in the Form tag itself. I tried removing the Register button and setting an onclick on the and calling a function that redirected to the register page. That didn't even work, and the enter key still redirected to the register page.
They are placed in the LayoutTemplate like this:
<div class="btn-login">
<asp:LinkButton ID="LoginButton" runat="server" CommandName="Login" ValidationGroup="LoginUserValidationGroup"
Height="38px" Width="120px">
</asp:LinkButton>
</div>
<div class="btn-opretbruger">
<asp:LinkButton ID="Register" runat="server" ValidationGroup="LoginUserValidationGroup"
Height="38px" Width="120px" PostBackUrl="/Account/registrerbruger.aspx">
</asp:LinkButton>
</div>
All the articles I have read used Button controls, and I use a LinkButton.
Any ideas?
Task 1)
Add some scrip to your page to disable the enter key...
function stopRKey(evt) {
var evt = (evt) ? evt : ((event) ? event : null);
var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
if ((evt.keyCode == 13) && (node.type == "text")) { return false; }
}
Task 2)
Add some script that allows you to target a html element and raise the event handler for it
function submitByTarget(event, target) {
if (event.keyCode == 13) {
jQuery('#' + target.id).click();
}
}
Task 3)
Add an event handler for your login txtbox that waits for the enter key to be hit and raises the correct .click handler for the target button...in this case 'login'
txtLogin.Attributes.Add("onkeypress", "return submitByTarget(event," + btnSearch.ClientID + ");");
Include the following code in the page load even.It'll automatically set the focus on the button.
//Set the default button for the Login page
this.Page.Form.DefaultButton = btnLogin.UniqueID;
精彩评论