Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to set focus at the end of textbox while typing?
    primarykey
    data
    text
    <p>I have a textbox with a live search function. It is working all good except one problem. If I type any characters on it, it just loses its focus. If I set textbox.Focus(), the cursor goes at the beginning of the textbox.</p> <p>I have tried most of solutions on the internet. Please check my codes below.</p> <pre><code>asp:TextBox ID="searchCompany" runat="server" Text="" CssClass="searchCompany" AutoPostBack="true" Width="190px" OnTextChanged="searchCompany_TextChanged"&gt;&lt;/asp:TextBox&gt; </code></pre> <p>In page_Load</p> <pre><code>protected void Page_Load(object sender, EventArgs e) { //ScriptManager1.RegisterAsyncPostBackControl(Menu1); menuDisplay(); searchCompany.Attributes.Add("onkeyup", "setTimeout('__doPostBack(\\'" + searchCompany.UniqueID + "\\',\\'\\')', 0);"); //searchCompany.Attributes.Add("onfocus", "javascript:setSelectionRange('" + "','')"); //searchCompany.Focus(); } </code></pre> <p>and I have tried javascript as below</p> <pre><code>&lt;script type="text/javascript"&gt; function setSelectionRange() { var inputField = document.getElementById('searchCompany'); if (inputField != null &amp;&amp; inputField.value.length &gt; 0) { if (inputField.createTextRange) { var FieldRange = inputField.createTextRange(); FieldRange.moveStart('character',inputField.value.length); FieldRange.collapse(); FieldRange.select(); } } } &lt;/script&gt; </code></pre> <p>I have tried to put codes on a method "searchCompany_TextChanged" which is calling if user type any characters on a textbox everytime however it is not working as well.</p> <p>I saw other solutions with using Textbox.Select() but System.Windows.Control is not working in asp.net i guess.</p> <p>Any idea??</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload