MaskedEditExtender太难用钱了

时间:2009-09-25 14:23:20

标签: asp.net javascript asp.net-ajax maskedtextbox maskededitextender

MaskedEditExtender可以很好地执行规则,但是我的用户在输入TextBox时遇到了麻烦。

我希望在获得焦点时选择TextBox的所有内容。

常规JavaScript解决方案不起作用。

onfocus="javascript:this.select();"

MaskedEditExtender会干扰。

如何在获得焦点时选择TextBox的所有内容?

<asp:TextBox
    ID="TextBoxPrice"
    runat="server" />
<ajaxToolkit:MaskedEditExtender
    ID="MaskedEditExtenderTextBoxPrice"
    runat="server"
    TargetControlID="TextBoxPrice"
    Mask="9,999.99"
    MaskType="Number"
    MessageValidatorTip="False"
    OnFocusCssClass="MaskedEditFocus"
    OnInvalidCssClass="MaskedEditError"
    InputDirection="RightToLeft"
    AcceptNegative="None"
    DisplayMoney="Left" />
<ajaxToolkit:MaskedEditValidator
    ID="MaskedEditValidatorTextBoxPrice"
    runat="server"
    ControlToValidate="TextBoxPrice"
    ControlExtender="MaskedEditExtenderTextBoxPrice"
    Display="Dynamic"
    IsValidEmpty="False"
    EmptyValueMessage="Price is required"
    InvalidValueMessage="Price is invalid"
    MinimumValue= "0.01"
    MinimumValueMessage="Price is too small"
    MaximumValue="9999.99" 
    MaximumValueMessage="Price is too large" />

1 个答案:

答案 0 :(得分:0)

使用此脚本:

<script type="text/javascript">

        function selectAllCharsBefore(inputText, char) {


            setTimeout(function () {
                if (!inputText) return false;

                var indexChar = inputText.value.indexOf(char);
                if (indexChar != -1) createSelection(inputText, 0, indexChar)
            }, 100);


            return true;


        }

        function whatDecimalSeparator() {

            var n = 1.1;

            n = n.toLocaleString().substring(1, 2);

            return n;

        }

        function createSelection(field, start, end) {
            if (field.createTextRange) {
                var selRange = field.createTextRange();
                selRange.collapse(true);
                selRange.moveStart('character', start);
                selRange.moveEnd('character', end);
                selRange.select();
                field.focus();
            } else if (field.setSelectionRange) {
                field.focus();
                field.setSelectionRange(start, end);
            } else if (typeof field.selectionStart != 'undefined') {
                field.selectionStart = start;
                field.selectionEnd = end;
                field.focus();
            }
        }


    </script>