JQuery Autocomplete strange focus behaviour - c#

I'm trying to show data into one JQuery Autocomplete Dropdown. The information is retrieved from one dynamic ASPX:
$("#<%= this.txt.ClientID %>").autocomplete('<%=this.ResolveUrl("~/Page.aspx") %>')
Obviously I don't want make more GETs than necessary, so I have a bit of control with a Java Timer (on every keypress):
if (timerActive) {
stopTimer();
}
timer = setTimeout('obtainItems()', 800);
timerActive = true;
And the obtain items does:
$("#<%= this.txt.ClientID %>").autocomplete('<%=this.ResolveUrl("~/Page.aspx") %>')
stopTimer();
This thing works in general, but I've seen some special behaviour with arrows and AvPag. When the records of the dropdown are displayed, if I press AvPag the component selects the correct record, but after two seconds comes back to the previous one.
My first theory was that the .autocomplete was resetting the focus, but I have noticed that this is not correct. I thought that it was a bug (BUG LINK) but it doesn't work too.
Please, if somebody can help... thanks a lot!

Does it work as desired without the timer?
I'm guessing your onkeypress event is fired, user selects what they want, then the timer event fires which resets the items.
add a variable
var selected = false;
and attach a selected event to your autocomplete and set selected = true
have your obtainItem look like this:
function obtainItems()
{
if (!selected)
{
$("#<%= this.txt.ClientID %>").autocomplete('<%=this.ResolveUrl("~/Page.aspx") %>')
stopTimer();
}
}

Related

AutoCompleteExtender - PageMethod has no data to return

I am making use of the AutoCompleteExtender and need know how to handle the scenario when the PageMethod has no data to return. I am currently making use of 'OnClientPopulating' and 'OnClientPopulated'
In the 'OnclientPopulating' I am doing the following:
function suggestionListPopulating(source, e) {
var textboxControl = $(source.get_element());
textboxControl.css('background', '#FFF url(images/loading3.gif)no-repeat right');
}
In the 'OnclientPopulated' I am doing the following:
function suggestionListPopulated(source, e) {
var textboxControl = $(source.get_element());
textboxControl.css('background-image', 'none');
}
Of course, since no data is being returned in certain; which is fine and correct, the 'OnClientPopulated' event is never firing, thus leaving a background image in my textbox.
Is there a way to clear the texbox background if the textbox control is not populated? Meaning a list never pop-ups to select from.
I found a descent workaround if anyone is interested.
http://www.aspforums.net/Threads/120948/ASPNet-AJAX-AutoCompleteExtender-Display-No-records-found-message-when-no-matches-found/

SendKeys.Send function not working

I want to press Shift + Tab on some event , I am using System.Windows.Forms.SendKeys.Send for that purpose but it is not working , I tried below ways to call the function.
System.Windows.Forms.Application.DoEvents();
SendKeys.Send("{+(Tab)}");
System.Windows.Forms.Application.DoEvents();
SendKeys.Send("+{Tab}");
System.Windows.Forms.Application.DoEvents();
SendKeys.Send("{+}{Tab}");
System.Windows.Forms.Application.DoEvents();
SendKeys.Send("+{Tab 1}");
Can someone tell me what is the right way ?
The proper syntax is:
SendKeys.Send("+{Tab}");
In light of your comment that you are trying to implement pressing Shift+Tab to cycle between control fields, note that this can be done more reliably without emulating keys. This avoids issues where, for instance, another window has focus.
The following method will emulate the behavior of Shift_Tab, cycling through tab stops in reverse order:
void EmulateShiftTab()
{
// get all form elements that can be focused
var tabcontrols = this.Controls.Cast<Control>()
.Where(a => a.CanFocus)
.OrderBy(a => a.TabIndex);
// get the last control before the current focused element
var lastcontrol =
tabcontrols
.TakeWhile(a => !a.Focused)
.LastOrDefault(a => a.TabStop);
// if no control or the first control on the page is focused,
// select the last control on the page
if (lastcontrol == null)
lastcontrol = tabcontrols.LastOrDefault();
// change focus to the proper control
if (lastcontrol != null)
lastcontrol.Focus();
}
Edit
The deleted text will cycle through controls in reverse order (emulating shift+Tab), but this is more properly done with with the built-in Form.SelectNextControl method. The following method will emulate the behavior of Shift_Tab, cycling through tab stops in reverse order.
void EmulateShiftTab()
{
this.SelectNextControl(
ActiveControl,
forward: false,
tabStopOnly:true,
nested: true,
wrap:true);
}
Is it does nothing or sends input into control you don't want to be edited? Check if this code is called first, and dont't forget to focus on target control manually before SendKeys to make sure that it will receive your keys.

How to have a textbox click event in asp.net with c#

if (Request["__EVENTARGUMENT"] != null && Request["__EVENTARGUMENT"] == "txt2OnClick")
{
txt2_Click();
}
txt2.Attributes.Add("onclick", this.Page.ClientScript.GetPostBackEventReference(txt2, "txt2OnClick")); //within page load
private void txt2_Click()
{
ImageMap1.ImageUrl = "guide/2.jpg";
}
This is a perfect piece of code to have a click event on textbox, with asp.net (C#).
But the only problem is, we cant type when we apply this code to a textbox. So what I did was set the focus txt2.Focus(); then I can type, but the textbox is not getting validated (I have added a regular expression validator) . Any help? Even to have a better onClick event for a textbox than this?
I don't see any point in having click event for text box.. Remove your 'perfect piece of code' and txt2 if not required..If you have added regular expression validator correctly, it will take care of validation..
EDIT:
For changing some text or showing some image, you don't need to post back to server..
You can either use javascript Focus event or jquery to do that..
In the link you provided, all those photos and texts are placed on divs and are already loaded on browser and when textbox gets focus, those div styles are just toggled between none and block..
Something like this(jsFiddle)..

JQuery Auto-Complete With TextChanged / OnBlur PostBack

I'm having a pretty nasty problem with an auto-completing textbox. I want to initiate an asynchronous PostBack whenever a user selects an item from the auto-completed field and retain the value of the item, rather than the text inputted. This works perfectly when enter is pressed rather than a mouse click.
An example of my issue:
Someone goes to the page, and types 1000 into the textbox. The autocomplete displays 10002, 1000B, and 10000. The user clicks on 1000B and an asynchronous PostBack initiates. Instead of 1000B, the TextBox.Text value is still 1000.
My assumption is that the textbox is initiating the PostBack before the value is actually getting assigned to it. I'm just curious if anyone has any possible solutions for what I'm talking about.
I fixed it in this manner:
As per another question on the site I added an autoPostBack parameter to the options list.
In bottom of the SelectCurrent() function, I added these lines.
if (options.autoPostBackSelection == true) {
__doPostBack($input.id, "");
}
Then, my blur function looked like this:
.blur(function() {
hasFocus = 0;
if (!config.mouseDownOnSelect) {
hideResults();
}
if (options.autoPostBackSelection == true) {
selectCurrent();
}
I actually struggled with this for a bit, my Javascript/DOM event skills aren't very good. Hopefully this helps someone.

C# Checkbox has strange behavior

When a winform first displays, the checkbox is unchecked by default. If when the form first displays, I click on the checkbox to 'check' it, the checkbox appears checked for a split second and then disappears. The checkedchanged event never fires. However, if anytime after the first initial attempt I click on the checkbox, the value changes (checked to unchecked and vice versa) like it should and the event fires.
Any idea why the checkbox would not check on the first attempt? It appears selected the first time when you hover over it so I know it has focus.
Update: it doesn't matter if you enter data into all other controls first and then click on the checkbox, the first time you click on it, it flashes as checked for a second, and then the check disappears. Anytime after the 1st time though it works. Strange...
Hard to tell without seeing a code snippet. When I've had things like this in the past, it's been due to having duplicate control ids, or wiring up event handlers incorrectly. Have you tried disabling portions of your code and seeing what affects the checkbox behaviour?
Strangely, putting code in the CheckedChanged() to set the value (what it gets set to anyway if I trace through it) seems to work:
if (this.chkbox1.Checked == true)
{
this.chkbox1.Value = "1";
this.chkbox1.Text = "Checked";
}
else
{
this.chkbox1.Value = "0";
this.chkbox1.Text = "Un-checked";
}
I also put a focus() in the click():
if (((System.Windows.Forms.MouseEventArgs)(e)).Clicks <= 1)
{
if (this.chkbox1.Focused == false)
{
this.chkbox1.Focus();
}
}
I have no idea why that fixes the problem, but it does.
do this happen with just one CB? or all the CB on the form.
Have you tried deleting the CB then adding it back?
I would suggest you post the code behind the CB?
For your custom code, I would try using different cast styles, to see if it makes any difference.
If you're using the standard C# syntax, and it fails (vanishing checkbox)
CheckBox checkBox = sender as CheckBox;
I would try using the old-style cast on the sender object and see if it gives you the desired result:
CheckBox checkBox = (CheckBox)sender;
This may give you a hint on the root cause.

Categories