0

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

在此处输入图像描述

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

每个字段都有一组规则(在 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();
}

但是验证之后仍然处于活动状态并阻止链接。我如何真正停用客户端的验证?

4

1 回答 1

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" />
于 2014-04-14T18:33:04.480 回答