【问题标题】:Validation Message By JavascriptJavascript验证消息
【发布时间】:2012-03-30 01:49:33
【问题描述】:

我有一个问题。选中复选框时如何禁用验证。我试过 ValidatorEnable(control, false) ,它对我不起作用。

<dxe:BaseCheckBox runat ="server" ID="chkIsChecked" ClientInstanceName ="chkIsChecked">
       <ClientSideEvents CheckedChanged="OnSelectionChanged"/>
</dxe:BaseCheckBox>


<dxe:BaseComboBox runat ="server" ID="cboLabour" ClientInstanceName ="cboLabour" ValueType="System.String" ClientEnabled="false"  >
     <ValidationSettings SetFocusOnError="True" ErrorText="This field is required to fill."
       ErrorDisplayMode="ImageWithTooltip" ValidationGroup="RejectReason"   
       CausesValidation="false">
          <RequiredField IsRequired="True" ErrorText="This field is required to fill.">
          </RequiredField>
     </ValidationSettings>
</dxe:BaseComboBox>

我的 Javascript:

function OnSelectionChanged(s,e)
{
    if(chkIsChecked.GetValue())
    {
        cboLabour.SetEnabled(true);
    }
    else
    {      
        cboLabour.SetEnabled(false);
        cboLabour.SetValue('');
    }
}

感谢您的建议。

【问题讨论】:

  • 由于您没有使用内置控件,您应该指定这些控件来自哪个库。答案取决于它。

标签: c# javascript asp.net validation devexpress


【解决方案1】:

为此,您需要使用自定义验证。像这样的:

function OnLabouryValidation(s, e) {
     if (chkIsChecked.GetValue())
         return;

     var v = e.value;
     if(e.value)
          return;

    e.isValid = false;
    e.errorText = "Required.";
}

这里有关于自定义验证的“文档”,但在我的快速阅读中,并不清楚如何将其连接起来。

http://documentation.devexpress.com/#AspNet/CustomDocument11135

【讨论】:

    【解决方案2】:

    正如我从您的问题中发现的那样,您正在寻找组验证:

    首先在 ValidationSettings 中设置 ValidateOnLeave="False" 并为您的控件指定验证组名称,然后使用客户端 ASPxClientEdit.ValidateGroup('MyGroup');method 通过根据您的要求进行一些自定义来验证您的控件组。

    标记:

     <dxe:BaseCheckBox runat ="server" ID="chkIsChecked" ClientInstanceName ="chkIsChecked">
               <ClientSideEvents CheckedChanged="OnSelectionChanged"/>
        </dxe:BaseCheckBox>
    
    
        <dxe:BaseComboBox runat ="server" ID="cboLabour" ClientInstanceName ="cboLabour" ValueType="System.String" ClientEnabled="false"  >
             <ValidationSettings SetFocusOnError="True" ErrorText="This field is required to fill."
               ErrorDisplayMode="ImageWithTooltip" ValidationGroup="RejectReason"   
              ValidateOnLeave="False">
                  <RequiredField IsRequired="True" ErrorText="This field is required to fill.">
                  </RequiredField>
             </ValidationSettings>
        </dxe:BaseComboBox>
    
    
    <dx:ASPxButton ID="btn" runat="server" Text="Validate" 
    AutoPostBack="False" CausesValidation="False">
    
             <ClientSideEvents Click="function(s, e) {
    
        if (chkIsChecked.GetValue())  // if checked then validate the group
        {
                  ASPxClientEdit.ValidateGroup('RejectReason');
        }
             }" />
        </dx:ASPxButton>
    

    遵循这些文档并实施您想做的任何事情。
    How to: Validate a Group of Editors
    How to: Validate All Editors on a Page
    How to: Implement a Custom Validation
    ASPxClientEdit.Validation Event

    【讨论】:

    • 我在 btn 中添加了按钮单击事件,即 OnClick="btnSubmit_Click" ,当我单击按钮后,页面成功验证了组合框,但它会触发 btnSubmit_click 事件。
    • 你用过 AutoPostBack="False" 它会停止回发
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-19
    • 2014-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多