Thursday, February 5, 2015

Using a field mask and forcing the cursor to the left

You can achieve that using javascript and jQuery by setting the SelectionRange to 0,0 on the mouseup event:
$(document).ready(function(){
   $(".resetCursor").mouseup(function () {
      this.setSelectionRange(0,0);    
   });
});
Then add the class resetCursor to the TextBox Controls you want to extend with this behavior.
<asp:TextBox ID="txtePro" runat="server" 
   CssClass="textbox resetCursor" 
   Font-Size="Small" 
   Height="18px" 
   Width="100px" 
   Visible="False" 
   Wrap="False" 
   OnTextChanged="txtEPro_OnLeave" 
   AutoPostBack="true">
</asp:TextBox>
Explanation:
resetCursor is a css stylesheet class that is used by jQuery to select the specific DOM-Elements. It is common practice to use css classes to "declare" certain behavior on a set of DOM-Elements and then use jquery to "activate" that behavior in one go. Edit: Note that resetCursor is an arbitrary name, you could have chosen another name as you please.
Here is a good introduction to the basic jQuery selectors (I'm using the Class-Selector in this example):

No comments:

Post a Comment