I have a form with a default button set.So when i press enter,form is submitted regardless of the focus. My requirement is when focus is inside fileupload control and enter is pressed,instead of submitting the form show browse dialog. So I created a panel and attached a onKeyPress event to this panel
fileUploadDocument.Attributes.Add("onkeyPress", "return ByPassEnter(event);");
<asp:Panel ID="fileUploadDocument" runat="server">
<div>
<uc2:FileUploadControl ID="ucdocxUploadControl" runat="server" ErrorMessageNoFileSelected="Please select a file for upload."
ValidationGroup="BrandLogoFileUpload" />
</div>
</asp:Panel>
function ByPassEnter(e) {
var evt = (e) ? e : window.event;
var key = (evt.keyCode) ? evt.keyCode : evt.which;
if (key == 13) {
document.getElementById('<%=ucdocxUploadControl.BaseFileUploadControl.ClientID %>').click();
CancelDefault(evt);
}
}
function CancelDefault(e) {
e.cancel = true;
if (e.preventDefault) {
e.preventDefault();
}
e.returnValue = false;
}
When i press enter on fileupload control, browse dialog shows up and when i choose a file or hit cancel on browse dialog,postback happens. e.returnValue = false should prevent the postback but even then postback happens.
However if i change onkeypress to onkeydown,everything works and postback doesnot happen.Can somebody tell me the difference in these events and why postback is happening in onkeypress.
The keypress event fires after the key is pressed and released. At that point your form's submit action is activated.
keydown fires after the press but before the release. At that point you can cancel the keypress from registering.
Related
I am working on a user control (ascx) with an asp TextBox control.
Whenever I press the enter key on the TextBox, the user control automatically submits it.
I do not want that to happen. I want to disable automatic submit on press of the enter key on any other control aside from the Submit Button.
Please note that I do not use any HTML controls aside from Table.
Thanks!
You can put javascript code and disable Enter Key event ...
<div onkeypress="return noenter(event)">
javascript code :
<script language="javascript" type="text/javascript">
function noenter(e) {
e = e || window.event;
var key = e.keyCode || e.charCode;
return key !== 13;
}
</script>
I have a Telerik Radgrid containing all the valid usernames and passwords that can be used to unlock functionnalities on my web page.
The password is encrypted, so you have to click the row to decrypt and show the actual password. All usernames and passwords can be changed by an admin
When the admin presses [ENTER] to submit the new username/password combination (instead of pressing the submit button), the new combination is actually submitted (which is fine).
However, my problem is that, by default, the RadGrid selects the next row, which decrypts the password and lets you edit the username/password.
Ideally, pressing [ENTER] would only submit the username/password combination, not selecting any other row. If that's not possible, pressing [ENTER] should not do anything.
If have tried the following javascript code:
<script type="text/javascript">
function KeyPressed(sender, eventArgs) {
if (eventArgs.get_keyCode() == 13)
{
alert("Cancelling event");
eventArgs.set_cancel(true);
}
}
</script>
The alert box is raised, but the [ENTER] event is not interrupted.
Edit: Note that the alert is for testing only!
I have also added this code to my RadGrid control to add KeyPress capture
<ClientSettings EnableRowHoverStyle="true" EnablePostBackOnRowClick="true" ClientEvents-OnKeyPress="KeyPressed" AllowKeyboardNavigation="true">
<Selecting AllowRowSelect="True" />
</ClientSettings>
My question is: How can I interrupt the [ENTER] keypress event? Or modify its behavior?
Any hints will be appreciated
I am using VB.net on Visual Studio 2008.
I am using ASP.net version 2.0
Here is my javascript code to cancel an event on enterkey.
function CheckKey() {
if (event.keyCode == 13) {
CancelEvent();
}
}
function CancelEvent() {
var e = window.event;
e.cancelBubble = true;
e.returnValue = false;
if (e.stopPropagation) {
e.stopPropagation();
e.preventDefault();
}
}
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 button 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;
I am trying to assign the Enter key to a button inside of a gridview. Does anyone know how this can be accomplished? Thank you.
You can give the button an id on one of the rendering events of the GridView, and then with jQuery bind the keypress() to a main div that wraps your site, and have it simulate the click() event on the button in your gridview when the 'enter' key (keyCode 13) was detected.
Like this :
$(document).ready(function() {
$('#mainDivWrapperId').keypress(function(event) {
if (event.keyCode == '13') {
$('#buttonId').click();
}
});
});
Edited Addition :
If you have a button in a gridview, then you just need to catch the 'enter' key press event like I've shown above, and then you can have it trigger the 'click' event of your button.
Like this :
$(document).ready(function() {
$('#mainDivWrapperId').keypress(function(event) {
if (event.keyCode == '13') {
$('#buttonId').trigger('click');
}
});
});
actually to make it simple,
1st declare the button on click event handler.
2nd declare a on keydown/keypress event handler for grid view.
inside the gridview's keypress/keydown event handler, check for keycode 13 from the event argument.
if the event.keycode==13, do button.click().
what ever logic inside the button's click event handler will be processed.
I have an ASP.NET application where there's a few ASP.NET buttons and several plain HTML buttons. Anytime there's a textbox where a user hits enter, the ASP.NET button tries to submit the form.
I know I can change the defaultButton, but I don't want there to be any default button. I just want it so when the user presses enter it doesn't do anything.
I've tried setting defaultButton to blank, but that doesn't seem to work. How do I prevent the form from being submitted by the ASP.NET button when enter is pressed?
You can set the button's UseSubmitBehavior = false
btnCategory.UseSubmitBehavior = false;
Here is what I used to fix this problem.
<form runat="server" defaultbutton="DoNothing">
<asp:Button ID="DoNothing" runat="server" Enabled="false" style="display: none;" />
I also had this problem with an ImageButton on my MasterPage (the Logout button) being set as the default button (so whenever someone pressed Enter, it would log them out). I solved it by using the following line in the Page_Load event on every child page, which is a bit of a work-around, but it works:
Form.DefaultButton = cmdSubmit.UniqueID;
Hope this helps someone else.
<asp:TextBox ID="TXT_Quality" runat="server" Width="257px"
onkeypress="return key_Pressed(event, this);">
</asp:TextBox>
function key_Pressed(e, textarea)
{
var code = (e.keyCode ? e.keyCode : e.which);
if(code == 13)
{
return false;
}
return true;
}