Wednesday 11 January 2017

C# AJAX TextBox Cursor,Caret Focus with AutoPostBack="True" OnTextChanged="TextBox1_TextChanged"

Hi All, here explain how to mantain get and set cursor/caret position after a c# with AJAX Textbox TextChanged with AutoPostBack="True", this solution is cross browser compatible.
Put this Javascript code in your *.aspx Page:

<script language="javascript" type="text/javascript" >
//lwebcode.bloggger.com
//http://blog.vishalon.net/javascript-getting-and-setting-caret-position-in-textarea
var selstart=0;
var selend=0;

 var pos = 0;
 function GetPos(el)
 {
    if("selectionStart" in el) {
       pos = el.selectionStart;
    } else if("selection" in document) {
       el.focus();
       var Sel = document.selection.createRange();
       var SelLength = document.selection.createRange().text.length;
       Sel.moveStart("character", -el.value.length);
       pos = Sel.text.length - SelLength;
    }
    return pos;
 }

 function SetPos(ctrl, mypos)
 {
        if(ctrl.setSelectionRange)
        {
            ctrl.focus();
            ctrl.setSelectionRange(mypos, mypos);
        }
        else if (ctrl.createTextRange)
        {
            var range = ctrl.createTextRange();
            range.collapse(true);
            range.moveEnd('character', mypos);
            range.moveStart('character', mypos);
            range.select();
        }
  }
</script>


Always in your *.aspx page:

<asp:TextBox ID="TextBox1" runat="server" AutoPostBack="True" OnTextChanged="TextBox1_TextChanged" > 
<asp:TextBox ID="TextBox2" runat="server" AutoPostBack="True" OnTextChanged="TextBox2_TextChanged" >

On Page Code behind *.apsx.cs

private const string SCRIPT_DOFOCUS =
        @"window.setTimeout('DoFocus()', 1);
    function DoFocus()
    {
        try {
            document.getElementById('REQUEST_LASTFOCUS').focus();
            SetPos(document.getElementById('REQUEST_LASTFOCUS'), pos);
        } catch (ex) {}
    }";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            HookOnFocus(this.Page as Control);
        }
        ScriptManager.RegisterStartupScript(this, typeof(Page), "ScriptDoFocus", SCRIPT_DOFOCUS.Replace("REQUEST_LASTFOCUS", Request["__LASTFOCUS"]), true);
    }
   
    private void HookOnFocus(Control CurrentControl)
    {
        //checks if control is one of TextBox, DropDownList, ListBox or Button
        if ((CurrentControl is TextBox)
            /*|| 
            (CurrentControl is DropDownList) ||
            (CurrentControl is ListBox) ||
            (CurrentControl is Button)*/
           
            )
            //adds a script which saves active control on receiving focus
            //in the hidden field __LASTFOCUS.
            (CurrentControl as WebControl).Attributes.Add(
               "onfocus",
               "try{document.getElementById('__LASTFOCUS').value=this.id; GetPos(this);} catch(e) {}");
        //checks if the control has children
        if (CurrentControl.HasControls())
            //if yes do them all recursively
            foreach (Control CurrentChildControl in CurrentControl.Controls)
                HookOnFocus(CurrentChildControl);
    }


protected void TextBox1_TextChanged(object sender, EventArgs e) 
{ //your code... }
protected void TextBox2_TextChanged(object sender, EventArgs e) 
{ //your code... }

No comments:

Post a Comment