DefaultButton in ASP.NET forms - asp.net

What is the best solution of defaultButton and "Enter key pressed" for ASP.NET 2.0-3.5 forms?

Just add the "defaultbutton" attribute to the form and set it to the ID of the button you want to be the default.
<form defaultbutton="button1" runat="server">
<asp:textbox id="textbox1" runat="server"/>
<asp:button id="button1" text="Button1" runat="server"/>
</form>
NOTE: This only works in ASP.NET 2.0+

Since form submission on hitting the enter key is a part of life with HTML, you'll have to trap the Enter key using javascript and only allow it to go through when it's valid (such as within textareas). Check out http://brennan.offwhite.net/blog/2004/08/04/the-single-form-problem-with-aspnet/ for a good explanation.

Related

ASP.NET Webforms OnClientClick without enter submission

I have the following command button:
<asp:Button Text="Insert Value" UseSubmitBehavior="false" runat="server" OnClientClick="return Confirmation.value('insert');" CommandName="Insert" />
I am using UseSubmitBehavior="false" to prevent ASP.Net page enter key causing post back
I do not want to listen to the enter keyCode via javascript because enter is used to submit non-webform elements not related to the form
Apparently, when using a Command and UseSubmitBehavior="false" then OnClientClick doesnt work. If I turn on submit behavior it works as expected but then hitting enter on the page automatically tries to click the button.
I prefer not to listen for the click event in Jquery or in javascript, and prefer a webform solution. Possible a better way of prevent enter from submitting the form or a way for OnClientClick to work properly with no submit behavior
You may need to use Panel and keep all your form inside Panel Tag and set the DefaultButton value of Panel to Button. Like Below:
<asp:Panel ID="Panel1" runat="server" DefaultButton="Button1">
// other form elements
<asp:Button Text="Insert Value" ID="Button1" runat="server" OnClientClick="return Confirmation.value('insert');" />
</asp:Panel>

No KeyDown event on my VB.NET Webform

I'm new to VB.NET webform development, but an old VB/Access developer.
I've used Keydown, Keypress events before in my normal development but cannot find this event with this new web development project I'm starting.
I'm assuming it's something to do with the fact it's a web-form. However when I search I can't find others with this issue so thought I'd ask it here. Below is a screen-shot of the events I have on the text control on the web-form where I'm trying to put the keypress event.
(I wanted to attach my picture showing you the events in the list but I don't have 10 reputation points so won't let me include it).
Is this event not available for web-form development? Essentially what I want to do is have the page check that there is text is both the txtUsername and txtPassword controls before enabling the "Log In" command button.
All I have in the drop-down list for the control is:
(Declarations)
DataBinding
Disposed
Init
Load
PreRender
TextChanged
Unload
Consider using a RequiredFieldValidator:
<asp:TextBox id="Foo" runat="server"/>
<asp:RequiredFieldValidator id="Bar"
ControlToValidate="Foo"
Display="Static"
ErrorMessage="*"
runat="server"/>
And in your submit button's click handler:
If Page.IsValid Then
...
Else
...
End If
You will probably also want to use the HTML5 required attribute:
<asp:TextBox id="Foo" runat="server" required="required" />
You might also consider using aria-required:
<asp:TextBox id="Foo" runat="server" required="required" aria-required="true" />

Handle Enter Key for search and login page

I have an ASP.Net 3.5 in VB.Net page that uses a master-page and has several pages it uses to display content. On the master-page their is a search text-box for the site so that shows up on all the pages. Their is also a login page screen to get to a members account.
The issue is: When you are on a content page I would like when a user puts something in to search for and presses enter it will process the search request.
But....
If you are on the login page I would like to disable enter key for the search and if the user presses enter on the login page to process the login request.
Is their an easy way to accomplish this?
Create your form in an asp:panel and set the DefaultButton to be the button of the form you want to submit when enter is pressed
<asp:Panel DefaultButton="btnSubmit" runat="server">
<asp:TextBox ID="UserName" runat="server"/>
<asp:TextBox ID="Password" TextMode="Password" runat="server"/>
<asp:Button ID="btnSubmit" OnClick="btnSubmit_Click" Text="Sign In" runat="server"/>
</asp:Panel>
Note: asp:panel creates a div

How not to trigger RequiredFieldValidator on hidden field (TextBox)

I think this is a common problem. I have a form where I show/hide fields dynamically using jQuery, depending on some radio buttons.
I have RequiredFieldValidator's on all the fields, but I don't want them to be triggered if their ControlToValidate is hidden (using jQuery).
Is that possible? Thanks in advance.
EDIT: Here is the solution, thanks to Marek. It might not be very obvious if you have weird clientIDs because of MasterPages
This is the ASPX
<asp:TextBox ID="txtName" runat="server" />
<asp:RequiredFieldValidator ID="vldName" ControlToValidate="txtName" runat="server" ErrorMessage="You must enter Name!" />
...
<asp:Button ID="btnSubmit" runat="server" Text="Submit" />
This is the jQuery
$(function() {
$('#ctl00_cphContent_btnSubmit').click(function() {
if (!$('#ctl00_cphContent_txtName').is(':visible'))
ValidatorEnable(ctl00_cphContent_vldName, false);
});
});
Hope it will make someone's life easier
If I remember correctly there's a function called ValidatorEnable(validatorClientId, isEnabled) that allows you to disable/enable the ASP.NET validators via javascript. You could use jQuery right before your form submit to disable all your invisible validators.
There's some documentation about client side API available from the validators here http://msdn.microsoft.com/en-us/library/aa479045.aspx
Hey u can set the control to validate property from C# code when u r setting visiblility from C# code.This wil solves ur problem.
if ($('#input-name').length) {
// do something
}
This will now solves ur problem

IE 8 - ASP.NET form not submitting when user presses enter key

I have a simple form written in asp.net/C# and when trying to hit enter while in the form's input box doesn't submit the form for some reason. I had implemented a fix for a previous bug where pressing enter would merely refresh the page without submitting the form data but now pressing enter just does nothing, the fix is below:
<div style="display: none">
<input type="text" name="hiddenText" />
</div>
anybody know about a fix for this or a workaround?
I'm assuming you have a button somewhere on your page, as well as an event handler for it.
Have you tried wrapping your form (with the button) inside a Panel control and setting the default button attribute?
i.e.
<asp:Panel id="pnlMyForm" runat="server" DefaultButton="btnMyButton">
<asp:textbox id="txtInput" runat="server" />
<asp:Button id="btnMyButton" text="Submit" runat="server" />
</asp:Panel>
You can specify a default button for a form, which means hitting enter on any input control will fire that button (i.e. target the submit button). I haven't heard of this not working in any specific browser. This should eliminate your need for a workaround/hack.
<form id="form1" runat="server">
<asp:Panel ID="pnlFormContents" runat="server" DefaultButton="btnSubmit">
<!-- add some input controls as needed -->
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click"/>
</asp:Panel>
</form>
Hope this helps...
I don't remember the specifics of the rules, but most browsers have the capability of submitting forms when ENTER is pressed if conditions are met. I think it had to do with whether you had 1 or more-than-one field, or whether or not there was at least one submit button (even if you hide it). I've done it in a site I recently did, but I don't have the code handy, but I can tell you it works without any special scripting. Check this posting for more details:
http://manfred.dschini.org/2007/09/20/submit-form-on-enter-key/

Resources