I’m using Visual Studio 2022 and an ASP.NET webforms app.
I have a textbox linked to a ajaxToolkit:MaskedEditExtender
.
If the user tabs from the previous control to the Text Box, and the textbox is empty, then the cursor is positioned to the left of the last "/".
If the user tabs from the previous control to the textbox, and the textbox is populated, then the cursor is positioned at the left.
How do I make the cursor be positioned at the left when the textbox is populated or empty when focus is set but not if the user has left mouse clicked on the control to position the cursor?
<asp:Label ID="lbJob_Number" runat="server" Text="Job Number:"></asp:Label>
<asp:TextBox ID="tbJob_Number" runat="server" Width="80px" AutoPostBack="True" ></asp:TextBox>
<ajaxToolkit:MaskedEditExtender ID="meeJob_Number" runat="server"
BehaviorID="meeJob_Number" CultureAMPMPlaceholder=""
CultureCurrencySymbolPlaceholder=""
CultureDateFormat="" CultureDatePlaceholder=""
CultureDecimalPlaceholder="" CultureThousandsPlaceholder=""
CultureTimePlaceholder=""
TargetControlID="tbJob_Number"
Mask="99/9999/???" MaskType="Number" ClearMaskOnLostFocus="false" />
<asp:Label ID="lbJob_Name" runat="server" Text="Job Name:"></asp:Label>
<asp:Label ID="lbJob_Name_Display" runat="server"></asp:Label>
<asp:Label ID="lbEstimating_Job_Number" runat="server" Text="Est Job No:" Visible="False"></asp:Label>
<asp:TextBox ID="tbEstimating_Job_Number" runat="server" Width="80px" AutoPostBack="True"></asp:TextBox>
<ajaxToolkit:MaskedEditExtender ID="meeEstimating_Job_Number"
runat="server" BehaviorID="meeEstimating_Job_Number"
CultureAMPMPlaceholder="" CultureCurrencySymbolPlaceholder=""
CultureDateFormat="" CultureDatePlaceholder=""
CultureDecimalPlaceholder="" CultureThousandsPlaceholder="" CultureTimePlaceholder=""
TargetControlID="tbEstimating_Job_Number"
Mask="99/9999/???" MaskType="Number" ClearMaskOnLostFocus = "false"/>
<asp:Label ID="lbEstimating_Job_Name" runat="server" Text="Est Job Name:" Visible="False"></asp:Label>
<asp:TextBox ID="tbEstimating_Job_Name" runat="server" Enabled="False" Visible="False" Width="300px" MaxLength="30"></asp:TextBox>
Thanks
2
Answers
I found this Posted by RK001 which says setting the Text Box onFocus
to a Script function
As i'm using a MaskEditExtender here's my working version:-
You could use
obj.selectionStart = obj.selectionEnd = position;
to move the cursor, whereobj
is yourTextBox
andposition
is the position you want to move to.In the following code, assuming the cursor is at
tbJob_Number
, pressTab
to switch the focus position, and you could find that the focus oftbEstimating_Job_Number
is at the far left of the text. When the user directly specifies the cursor position with the mouse,onfocus
will not affect the position of the cursor selected by the mouse.You could adjust it according to your needs.