Handle Enter Key for search and login page - asp.net

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

Related

Hide validations on form post back in aspx page

I have put server side validations for each text box using <asp:RequiredFieldValidator/>. I have called ClearFields() method on page load that will clear all fields on the form when the button is clicked. The problem is that when the form gets posted and the fields are cleared, the validation message appears again. How to hide the validation messages on form post back. I am sorry, but its been years I have not coded in aspx and I can't find any solution online.
This is the textbox code:
<asp:TextBox runat="server" CssClass="form-control" placeholder="Your Name *" ID="name"/>
<asp:RequiredFieldValidator runat="server" ControlToValidate="name" ErrorMessage="Name seems empty" CssClass="help-block text-danger"></asp:RequiredFieldValidator>
This is the button code:
<asp:Button runat="server" class="btn btn-xl" Text="Send Message" ID="submit" OnClick="submit_Click" CausesValidation="false"/>
I guess you are getting the validation message due to button used for clearing the fields. Set CausesValidation property of button to false:-
<asp:Button ID="ClearButton" runat="server" CausesValidation="false" Text="Clear" />
Also, please note asp:RequiredFieldValidator works on client side i.e on the browser it is not a server side validation.
Update:
Since you are clearing your fields on click of button, you can clear in submit_Click method itself after sending the email instead of page load. Ideally you should have a separate button to clear the form though.

asp.net: Setting up multiple forms on a page

I have a master page that contains a search box which is validated as a required field before the user can submit the search field.
The problem I'm having now is that one of my content pages has a DetailsView which won't let me edit a record, because the search box coming from the master page is blank.
The structure of the code is like this:
Master Page:
<form runat="server">
<asp:RequiredFieldValidator ID="SearchBoxRequiredFieldValidator"
runat="server" ControlToValidate="searchTextBox"
Display="None" ErrorMessage="Enter an employee's last name"></asp:RequiredFieldValidator>
<asp:TextBox ID="searchTextBox" autocomplete ="off" runat="server" Width="180px"></asp:TextBox>
<asp:Button ID="SearchButton" runat="server" Text="Employee Search"/>
<!--.....-->
<asp:ContentPlaceHolder ID="MainContent" runat="server"/>
</form>
The "MainContent" placeholder is populated with a page with only a DetailsView. How should I change my code so that I can submit forms from my MainContent pages, but also allow the Master page's search feature to function properly?
I'm pretty new to asp.net forms, so any help is greatly appreciated!
Take a look at ValidationGroups. You can separate each logical form into different validation groups giving the effect of multiple discrete forms.

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/

Adding 'onClientClick' javascript to an ASP.NET Login control

I've got to put a login page from a 3rd party website in an iframe on another website that I'm developing. I need to add some JavaScript to break out of the iframe when the user logs in, but I can't make the login button to execute the JavaScript and do the postback for the login - just one or the other.
Here's the code from the iframe'd login page that I'm triying to adapt:
<asp:LoginView ID="LoginView1" runat="server">
<AnonymousTemplate>
<asp:Login ID="Login1" runat="server" OnAuthenticate="Login1_Authenticate">
<LayoutTemplate>
<asp:Label ID="lblUsername" runat="server" AssociatedControlID="Username" Text="Email" />
<asp:TextBox ID="Username" runat="server" Text="myName" />
<asp:Label ID="lblPassword" runat="server" AssociatedControlID="Password" Text="Password" />
<asp:TextBox ID="Password" runat="server" Text="myPassword" />
<asp:ImageButton ID="btnLogin" runat="server" CommandName="Login" ImageUrl="~/Images/login-submit.gif" AlternateText="Login" OnClientClick="top.location.href = document.location.href; return true;" />
</LayoutTemplate>
</asp:Login>
</AnonymousTemplate>
<LoggedInTemplate>
You are currently logged in blurb..
</LoggedInTemplate>
Currently when the login button is clicked, the login page breaks out of the iframe, but then I have to click the button again to log the user in. Can anyone see what I'm doing wrong? Thanks.
top.location.href is setting the url of the browser to be a new url so you are never completing the action of the click.
What you could do is set the target of the form to be "_top"
something like
OnClientClick="document.getElementById('MYFORM_CLIENTID').target='_top';return true;"
Looking at your code I think that on the first click you are not logging in. You are just redirecting the parent window to your login page (the one which is displayed in the IFrame at first). That is why you need to click that button twice to log in - on the second time there is no redirect, as the parent window's URL does not change, so basically on the second time the top.location.href = document.location.href part does not yield any results and the login proceeds.
I think that the right course of action would be to authenticate first, then redirect, the opposite of how it looks currently. You can add a script on postback on that page, so that it checks if it's running in a frame and if so, it redirects the parent window to another page. Of course if you can modify the code... With the amount of detail that was provided I'm only able to tell what may be wrong and suggest a fix, but can't give a solution.

DefaultButton in ASP.NET forms

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.

Resources