<p>I'm using the <a href="http://www.asp.net/AJAX/AjaxControlToolkit/Samples/AutoComplete/AutoComplete.aspx" rel="nofollow noreferrer">AutoComplete</a> control from the ASP.NET AJAX Control Toolkit and I'm experiencing an issue where the AutoComplete does not populate when I set the focus to the assigned textbox.  </p>

<p>I've tried setting the focus in the Page_Load, Page_PreRender, and Page_Init events and the focus is set properly but the AutoComplete does not work.  If I don't set the focus, everything works fine but I'd like to set it so the users don't have that extra click.  </p>

<p>Is there a special place I need to set the focus or something else I need to do to make this work?  Thanks.</p>

[{'answer_id': 42858, 'author': 'Glenn Slaven', 'author_id': 2975, 'author_profile': 'https://Stackoverflow.com/users/2975', 'pm_score': 3, 'selected': True, 'text': '<p>We had exactly the same problem.  What we had to do is write a script at the bottom of the page that quickly blurs then refocuses to the textbox.  You can have a look at the (terribly hacky) solution here: <a href="http://www.drive.com.au" rel="nofollow noreferrer">http://www.drive.com.au</a> </p>\n\n<p>The textbox id is <code>MainSearchBox_SearchTextBox</code>.  Have a look at about line 586 &amp; you can see where I\'m wiring up all the events (I\'m actually using prototype for this bit.</p>\n\n<p>Basically on the focus event of the textbox I set a global var called <code>textBoxHasFocus</code> to true and on the blur event I set it to false.  The on the load event of the page I call this script:</p>\n\n<pre><code>if (textBoxHasFocus) {\n    $get("MainSearchBox_SearchTextBox").blur();\n    $get("MainSearchBox_SearchTextBox").focus();\n}  \n</code></pre>\n\n<p>This resets the textbox.  It\'s really dodgy, but it\'s the only solution I could find</p>\n'}, {'answer_id': 44006, 'author': 'Jason Shoulders', 'author_id': 1953, 'author_profile': 'https://Stackoverflow.com/users/1953', 'pm_score': 0, 'selected': False, 'text': '<p>How are you setting focus? I haven\'t tried the specific scenario you\'ve suggested, but here\'s how I set focus to my controls:</p>\n\n<pre><code>Public Sub SetFocus(ByVal ctrl As Control)\n    Dim sb As New System.Text.StringBuilder\n    Dim p As Control\n    p = ctrl.Parent\n    While (Not (p.GetType() Is GetType(System.Web.UI.HtmlControls.HtmlForm)))\n        p = p.Parent\n    End While\n    With sb\n        .Append("&lt;script language=\'JavaScript\'&gt;")\n        .Append("function SetFocus()")\n        .Append("{")\n        .Append("document.")\n        .Append(p.ClientID)\n        .Append("[\'")\n        .Append(ctrl.UniqueID)\n        .Append("\'].focus();")\n        .Append("}")\n        .Append("window.onload = SetFocus;")\n        .Append("")\n        .Append("&lt;/script")\n        .Append("&gt;")\n    End With\n    ctrl.Page.RegisterClientScriptBlock("SetFocus", sb.ToString())\nEnd Sub\n</code></pre>\n\n<p>So, I\'m not sure what method you\'re using, but if it\'s different than mine, give that a shot and see if you still have a problem or not.</p>\n'}, {'answer_id': 1059406, 'author': 'Kris', 'author_id': 1457761, 'author_profile': 'https://Stackoverflow.com/users/1457761', 'pm_score': 0, 'selected': False, 'text': '<p>What I normally do is register a clientside script to run the below setFocusTimeout method from my codebehind method. When this runs, it waits some small amount of time and then calls the method that actually sets focus (setFocus). It\'s terribly hackish, but it seems you have to go a route like this to stop AJAX from stealing your focus. </p>\n\n<pre><code>function setFocusTimeout(controlID) {\n    focusControlID = controlID;\n    setTimeout("setFocus(focusControlID)", 100);\n}\n\nfunction setFocus() {\n    document.getElementById(focusControlID).focus();\n}\n</code></pre>\n'}, {'answer_id': 2501610, 'author': 'Nathan Poorman', 'author_id': 300096, 'author_profile': 'https://Stackoverflow.com/users/300096', 'pm_score': 0, 'selected': False, 'text': "<p>I found the answers from Glenn Slaven and from Kris/Alex to get me closer to a solution to my particular problem with setting focus on an ASP.NET TextBox control that had an AutoCompleteExtender attached.  The document.getElementById(focusControlID).focus() kept throwing a javascript error that implied document.getElementById was returning a null object.  The focusControlID variable was returning the correct runtime ClientID value for the TextBox control. But for whatever reason, the document.getElementById function didn't like it.</p>\n\n<p>My solution was to throw jQuery into the mix, as I was already using it to paint the background of any control that had focus, plus forcing the Enter key to tab through the form instead of firing a postback. </p>\n\n<p>My setFocus function ended up looking like this:</p>\n\n<pre><code>function setFocus(focusControlID) {\n    $('#' + focusControlID).blur();\n    $('#' + focusControlID).focus();\n}\n</code></pre>\n\n<p>This got rid of the javascript runtime error, put focus on the desired TextBox control, and placed the cursor within the control as well.  Without first blurring then focusing, the control would be highlighted as if it had focus, but the cursor would not be sitting in the control yet.  The user would still have to click inside the control to begin editing, which would be an UX annoyance.</p>\n\n<p>I also had to increase the timeout from 100 to 300.  Your mileage my vary...</p>\n\n<p>I agree with everyone that this is a hack.  But from the end-user's perspective, they don't see this code.  The hack for them is if they have to manually click inside the control instead of just being automatically placed inside the control already and typing the first few letters to trigger the auto lookup functionality. So, hats off to all who provided their hacks.</p>\n\n<p>I hope this is helpful to someone else.</p>\n"}, {'answer_id': 8703236, 'author': 'paritosh', 'author_id': 1126540, 'author_profile': 'https://Stackoverflow.com/users/1126540', 'pm_score': 1, 'selected': False, 'text': '<p>this is waste , its simple </p>\n\n<p>this is what you need to do </p>\n\n<p>controlId.focus();  in C#\ncontrolID.focus()   in VB </p>\n\n<p>place this in page load or button_click section </p>\n\n<p>eg. panel1.focus();   if panel1 has model popup extender attached to it, then we put this code in page load section </p>\n'}]
['https://Stackoverflow.com/questions/42830', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2034/']