Auto Complete Textbox Enter to activate Search - c#

I have a Ajax auto complete extender attached to TextBox control. When the user starts typing the suggestive options are displayed in the divsion underneath. Getting the suggestion from a webservice call. On OnClientItemSelected="GetCode" I am using the below JavaScript to get the selected suggestion text into the search box.
Now I want the ENTER click to activate the search.
If the user selects from the suggestions he is getting and clicks the enter. It works fine. It activates the search. This is the code I am using.
function GetCode(source, eventArgs) {
var txtValue = document.getElementById('<%=txtAutoComplete.ClientID%>');
txtValue.value = eventArgs._value;
//$('#<%= txtAutoComplete.ClientID %>').val(eventArgs._value);
$('#<%= ImageButton1.ClientID %>').click();
}
But The problem is if the user doesnt select from the suggestion and types some text and clicks the enter key Doesnot activate search. Yes I know that It doesnot call the GetCode Function.
I am not getting how to do this. Anyone Please help me..
I am want something exactly like in www.laterooms.co.uk

You have to set a default button for either the form or the text field.

Related

Other text input methods?

There are a bunch of websites with "search" input boxes.
If I input a word manually then it automatically search without need to click any buttons.
Using the following code:
GeckoInputElement input = (GeckoInputElement)geckoHtmlElement;
input.Value = "searchword";
The I see that search input box is filled but nothing happens automatically.
If I manually add space or any character then website works as expected.
Auto searches my wanted word.
I tried using input.Focus(); but still same.
Any ideas how I can input text into search box in a more advanced way or something like that?
I think the solution here is to manually trigger the event that triggers the search - for example the keyup event. I had the same problem which I solved by adding this code:
nsAStringBase eventType = (nsAStringBase)new nsAString("keyup");
var ev = browser.Document.CreateEvent("HTMLEvents");
ev.DomEvent.InitEvent(eventType, false, false);
nameBox.GetEventTarget().DispatchEvent(ev);
You would need to inspect your page and see if there are any javascript events attached to your input.
Good luck!

StartupScript not triggered

Lets me explain the situation.
I have a gridview, when I hit an add button I show a popup window and let the user enter the info. When the info is completed, the user clicks OK and a client side comprobation is made and an e.processOnServer = isValid; with a true in there, so the server event runs (btnSubmitConfig_Click), and at the end of this function in code behind I need to insert this startup script to make an callback in the gridview to see that is populated, while the popup remains open with a message saying submit successful
My startup script is:
ScriptManager.RegisterStartupScript(this, GetType(), "Javascript", "aspxgridEditLog.PerformCallback(); ", true);
And is not fired.
Any guesses please?
Thanks
I found the problem, I had to insert this script in the Parent.Page, instead of this because it was trying to find the gridview in the popup, so it wasn't finding it.
Now works:
ScriptManager.RegisterStartupScript(this.Parent.Page, Parent.Page.GetType(), "Javascript", "aspxgridEditLog.PerformCallback(); ", true);
Thanks for the guesses, you helped me think about what was the problem.

Testing if a button has been clicked or not

I have an asp.net page which loads and writes values from a record to the controls on the page.
There is a link to another page and a save button.
If the user clicks save, fine, the changes are saved and they can navigate to wherever.
If they dont click save and click the link, any changes are lost.
Is there a way to check if the save has been clicked while the user has that page loaded?
I'm thinking along the lines of an OnClick Javascript function for the link such as -below, I'm not sure though how to test for the save button being clicked - can anyone point me in the right direction?
function handleHyperLinkClick(hyperlink)
{
confirm("You have not clicked save, any changes will be lost - click cancel then save your changes or ok to continue");
}
Add a bool variable, that is default false, set to true if the button has been clicked. Then check the value if user leaves the page.
You can do using hidden field. when user click on save button then set hidden field value.
You have to keep a temporary variable in javascript like state;
`
var state = 0;
//your function calls here
function yourFunction()
{
//// your coding
state =1;
//// your coding
}
`
In this case, you have to submit the value or navigate to other page when state=1, otherwise you shouldnt. This flag to be checked whilst clicking link (if its navigating via function) or onBeforeUnload event for page.
Please correct me know if you are looking for something other.
You can attach some value to a variable when the button is clicked. Then when the user tries to navigate, check whether that variable has that value. If so, it has been clicked, else not:
button = document.getElementById("button");
button.onclick = function() { clicked = true; }
document.getElementById("link").onclick = function() { if (!clicked) { alert("Sure?"); }
Assuming you are using ASP.NET server buttons like this:
<asp:Button runat="server" id="someButtonId" Click="someClickHandler" Text="Continue" />
If I were you, I would use jQuery to dynamically bind the click event of the button like this:
$(document).ready(function() {
$("input[type=button][id$=someButtonId]").on("click", function(e) {
if(!confirm("You have not clicked save, any changes will be lost - click cancel then save your changes or ok to continue"))
{
e.preventDefault()
}
});
});
Hope this helps.

Removing default text from text box

I got this Text box with default value as "First Name" ..Now when I click inside this text box to enter name , this value "First Name" keeps on displaying. What I want is to the text box to clear as soon as I click inside it. What property do I need to set in mt textbox tag ?
[Edit]
ok anything from Telerik that I can use to do that ?
There is not out of the box functionality in TextBox that will accomplish this, but the ASP.Net Ajax Toolkit has a Watermark Extender that will do everything you want.
I have used both, but now personally use a jQuery Watermark Plugin
Either will work just fine, choose based on your needs.
According to the Telerik docs you just have to set the EmptyMessage property on their TextBox control. Demo Page Here
In the code behind, on Page Load you can add the following code to achieve this
TextBox1.Attributes.Add("onClick", "javascript:if(this.value=='First Name'){this.value='';}");
You can use the method suggested by #Josh. If you do not want to use Ajax Toolkit controls or JQuery you could write it on your own using Javascript. Write a function which gets called when the foucs is received by the textbox control. I thik the function is called onfocus or just focus in Javascript.
Hi I just wrote this small function which will achieve your desired result
function clearInputBox(x,prefil){
if(x.value == prefil){
x.value = '';
}
}
Your input box looks like this
<input type='text' value='First Name' onfocus="clearInputBox(this,'First Name')" />
May be this will help you
Taking Shobans advice one step farther, you could add something like this to your Page subclass
protected override void OnInitComplete(EventArgs e)
{
string jsString = "javascript:if(this.value=='" + TextBox1.Text + "'){this.value='';}";
TextBox1.Attributes.Add("onFocus", jsString);
base.OnInitComplete(e);
}
What this will do is, it will always consider that default string is the one this controll contains at esign time (the initial one in your .aspx file), so you wont have to manually change it in codebehind every time you change your .aspx. Remember, that OnIinitComplete fires before any viewstate or postback data has been applied, but after the controlls on your page have been set to their default values.
P.S. As anishMarokey pointed, use onFocus vs onClick, since fields can gain focus without clicks via Tab key.

ASP.net Text Box Enter button pressed hell

Basically I have an asp.net website with login and search pages.
Currently, I have no idea why, when ever a user hits enter in either of the login text boxes (user name/password) or in the Seach text box, the website is redirected to the default page.
I have no idea why this is happening, I've tried setting defaultButton on both the panel containing the search and the login panel but that doesnt seem to work.
I've also tried catching the key press event with javascript which isnt working either.
I have no idea what event is being fired and why, or why it seems to override everything I try to do.
Anyone seen anything like this before?
---------------Edit-------------------
<div class="searchBar" onkeypress="javascript:return
WebForm_FireDefaultButton(event,'ctl00_MainContent_logView1_LogRepeater_ctl02_lbSearch')">
<input name="ctl00$MainContent$logView1$LogRepeater$ctl02$ctl02" type="text" />
<a id="ctl00_MainContent_logView1_LogRepeater_ctl02_lbSearch" href="javascript:__doPostBack('ctl00$MainContent$logView1$LogRepeater$ctl02$lbSearch','')">
Search</a>
</div>
Ok so here is the code generated by ASP.net that is getting ignored.
---------------Update----------------
I have tried adding a hidden button and setting that button as the defaultbutton of the form and that has stoped it from redirecting to the default page each time but this does mean that pressing enter in a text box just reloads the page, which is better but still isnt Ideal.
This may not be an ASP.NET quirk as much as it is a web-browser quirk.
When you press enter in a single-line text field, the browser will submit the form to the action specified in the form tag.
ASP.NET "helpfully" creates a form tag just inside the body tag and puts all body elements inside it...
As far as I know, you can safely remove this form tag; the HTML specification allows fields outside of form tags for use in JavaScript/AJAX operations.
I have seen this behaviour before, it occurred when the first button on the form was the LoginControl. Everytime enter was pressed it would fire the click event for the login and essentially log the user out. Are you using the LoginControl?
In the end I had to add a hidden button before the login control. The hidden button didn't do anything but it prevented the LoginControl from executing.
I had this same problem once. We had a login form that worked fine when a user hit enter. Then at one point we added a search box/button in the menu bar at the top of the page. Suddenly when users would hit enter they would attempt to perform a search. I resolved it by setting the .DefaultButton of the Form for the page to my login button.

Categories