禁用MaskedEditValidator和MaskedEditExtender

时间:2013-12-16 20:08:11

标签: javascript asp.net ajaxcontroltoolkit maskededitextender maskededitvalidator

我做了一个小表来允许记录的编辑,删除和插入:

enter image description here

表格(DataGridView,bound)非常简单:点击编辑进入“编辑”模式(提供“取消”),输入新值并点击“添加”以插入新记录。

每个字段都有一组规则(在x和y之间,都是数字),所以它们绑定到MaskedEditExtender和MaskedEditValidator:

(第1栏的例子):

<asp:TemplateField HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Right" FooterStyle-HorizontalAlign="Right" HeaderText="First bus #">
    <ItemTemplate>
        <asp:Label runat="server" ID="BusIdStartLabel" Text='<% #Bind("BusIdStart","{0:D4}") %>' />
    </ItemTemplate>
    <EditItemTemplate>
        <ajaxToolkit:MaskedEditExtender runat="server" ID="BusIdStartTextBoxMaskedEditExtender" TargetControlID="BusIdStartTextBox" BehaviorID="EditBusIdStartTextBoxBehavior"
                        MaskType="Number"
                        Mask="9999" PromptCharacter=" "
                        InputDirection="RightToLeft"
                        OnInvalidCssClass="maskedEditError" OnFocusCssClass="maskedEditFocus"
                        AutoComplete="False" ErrorTooltipEnabled="True" MessageValidatorTip="False" />
        <ajaxToolkit:MaskedEditValidator runat="server" ID="BusIdStartTextBoxMaskedEditValidator" ControlToValidate="BusIdStartTextBox" ControlExtender="BusIdStartTextBoxMaskedEditExtender"
                        MinimumValue="0" MaximumValue="9999"
                        IsValidEmpty="False" Display="None"
                        InvalidValueMessage="Enter a value between 0000 and 9999"
                        MaximumValueMessage="Enter a value between 0000 and 9999"
                        EmptyValueMessage="Enter a value between 0000 and 9999" />
        <asp:TextBox runat="server" ID="BusIdStartTextBox" Text='<%  #Bind("BusIdStart","{0:D4}") %>' Width="2.5em" Height="13px" CssClass="rightAlignTextBox" />
    </EditItemTemplate>
    <FooterTemplate>
        <ajaxToolkit:MaskedEditExtender runat="server" ID="BusIdStartTextBoxMaskedEditExtender" TargetControlID="BusIdStartTextBox" BehaviorID="FooterBusIdStartTextBoxBehavior"
                        MaskType="Number"
                        Mask="9999" PromptCharacter=" "
                        InputDirection="RightToLeft"
                        OnInvalidCssClass="maskedEditError" OnFocusCssClass="maskedEditFocus"
                        AutoComplete="False" ErrorTooltipEnabled="True" MessageValidatorTip="False" />
        <ajaxToolkit:MaskedEditValidator runat="server" ID="BusIdStartTextBoxMaskedEditValidator" ControlToValidate="BusIdStartTextBox" ControlExtender="BusIdStartTextBoxMaskedEditExtender"
                        MinimumValue="0" MaximumValue="9999"
                        IsValidEmpty="False" Display="None"
                        InvalidValueMessage="Enter a value between 0000 and 9999"
                        MaximumValueMessage="Enter a value between 0000 and 9999"
                        EmptyValueMessage="Enter a value between 0000 and 9999" />
        <asp:TextBox runat="server" ID="BusIdStartTextBox" Width="2.5em" Height="13px" CssClass="rightAlignTextBox" />
    </FooterTemplate>
</asp:TemplateField>

问题:点击表单的任何链接(编辑,删除)会触发所有内容的验证。因此,单击“编辑”会触发“插入”行的验证(返回“无效”)。我以为我会禁用它:

<asp:LinkButton runat="server" ID="ActionEditLink" CommandName="Edit" Text="Edit" OnClientClick="DeactivateValidation()" />

...

function DeactivateValidation() {
    TryDispose('EditBusIdStartTextBoxBehavior');
    TryDispose('FooterBusIdStartTextBoxBehavior');
    //etc.
}

function TryDispose(behaviorId) {
    var behavior = window.$find(behaviorId);
    if (behavior != null)
        behavior.dispose();
}

但之后验证仍然有效并阻止链接。我如何真正停用客户端的验证?

1 个答案:

答案 0 :(得分:0)

这样做的方法是使用CausesValidation并将其设置为False:

<asp:LinkButton runat="server" ID="ActionEditLink" CommandName="Edit" Text="Edit" CausesValidation="False" />
<asp:LinkButton runat="server" ID="ActionCancelLink" CommandName="Cancel" Text="Cancel" CausesValidation="False" />