Right after ASP validation fails, I cannot invoke the service method ONLY first time even if I provide the correct input

与世无争的帅哥 提交于 2020-01-25 10:14:15

问题


I have a small form with asp validations. After validation fails with the wrong input, I provide the correct input and press a button. Nothing happens with * still displayed indicating an error. However, when pressing button second time, the service is called.

This is the first scenario when providing the wrong Bin:

This is a second scenario:

When the input is correct and pressing "Search" button should cause the service method call, but it does not do anything. Only after I press on "Search" again it calls the service

This is my validators with the button:

   <span style="position:relative">
        <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" CssClass="searchValidator"  ErrorMessage="Bin is missing" ControlToValidate="txtIssuerSearch" ForeColor="Red" ValidationGroup="vgIssuerSearch">*</asp:RequiredFieldValidator>
        <asp:RegularExpressionValidator ID="RegularExpressionValidatorIssuerSearch" runat="server" CssClass="searchValidator" ErrorMessage="Invalid Bin Entered" ControlToValidate="txtIssuerSearch" ForeColor="Red" ValidationGroup="vgIssuerSearch" ValidationExpression="^([0-9]{6}|[0-9]{8})$">*</asp:RegularExpressionValidator>
   </span> 
       <asp:TextBox ID="txtIssuerSearch" runat="server"></asp:TextBox>
<asp:DropDownList ID="ddlIssuerSearch" runat="server">
<asp:ListItem>Name</asp:ListItem>
<asp:ListItem>Bin</asp:ListItem>
</asp:DropDownList>
<asp:Button ID="btnIssuerSearch" runat="server" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" 
    CausesValidation="false" CommandName="Search" Text="Search" OnCommand="btnIssuerSearch_Click" OnClientClick="return ValidateOnSearch()" />

and this is my javascript function:

function ValidateOnSearch() {

var isValid = false;
var ddlValue = $('#cphBody_ddlIssuerSearch :selected').text();

if (ddlValue == 'Name') {
    isValid = true;
}
else {
    isValid = Page_ClientValidate('vgIssuerSearch');
 }

if (!isValid) {
    $("#errorDisplaySearch").dialog({
        title: "Validation Error",
        modal: true,
        resizable: false,
        width: 250,
        buttons: {
            Close: function () {
                $(this).dialog('close');
            }
        }
    });
    return false;
}
return true;
}

What am I missing?


回答1:


I think you need to disable the RegularExpressionValidator when the Name value is selected in the dropdown.

Add onchange event handler on dropdown:

<asp:DropDownList ID="ddlIssuerSearch" runat="server" onchange="updateValidatorStatus(this.value)">

    updateValidatorStatus(selectedVal) {

    var regExpVal = document.getElementById('<%=RegularExpressionValidatorIssuerSearch.ClientID%>');

    if (selectedVal === 'Name') {
        ValidatorEnable(regExpVal, false);
    } else {
        ValidatorEnable(regExpVal, true);
    }
}


来源:https://stackoverflow.com/questions/55732080/right-after-asp-validation-fails-i-cannot-invoke-the-service-method-only-first

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!