当自动回发下拉列表触发时,验证消息仍然显示

发布于 2024-10-06 21:09:53 字数 941 浏览 3 评论 0原文

我有一个必需的字段验证器来验证下拉列表。这个下拉列表是一个自动回发列表,它的 Causevalidation 属性设置为 false。

问题是,当我选择默认项目时,会显示验证消息,但仍然会进行回发。回发后,消息消失。

这是代码片段:

<asp:RequiredFieldValidator ID="ContactMethodRequired" runat="server" ControlToValidate="ContactPreferences"
            Display="Dynamic" ErrorMessage="Please choose your contact method"
            EnableClientScript="true" InitialValue=""></asp:RequiredFieldValidator>
        <div>
            <asp:DropDownList ID="ContactPreferences" runat="server" AutoPostBack="true" CausesValidation="false">
                <asp:ListItem Text="Select" Value="" Selected="True"></asp:ListItem>                         
                <asp:ListItem Text="Email" Value="Email"></asp:ListItem>
                <asp:ListItem Text="Phone" Value="Phone"></asp:ListItem>
            </asp:DropDownList>
        </div>

I have a required field validator to validate a dropdownlist. this dropdownlist is an autopostback one, and it's causevalidation property is set to be false.

the issue is, when I select the default item, the validation message shows, but the still do the postback. And after the postback, the message disappers.

here is the snippet of codes:

<asp:RequiredFieldValidator ID="ContactMethodRequired" runat="server" ControlToValidate="ContactPreferences"
            Display="Dynamic" ErrorMessage="Please choose your contact method"
            EnableClientScript="true" InitialValue=""></asp:RequiredFieldValidator>
        <div>
            <asp:DropDownList ID="ContactPreferences" runat="server" AutoPostBack="true" CausesValidation="false">
                <asp:ListItem Text="Select" Value="" Selected="True"></asp:ListItem>                         
                <asp:ListItem Text="Email" Value="Email"></asp:ListItem>
                <asp:ListItem Text="Phone" Value="Phone"></asp:ListItem>
            </asp:DropDownList>
        </div>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

长梦不多时 2024-10-13 21:09:53
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title> 
<script type="text/javascript" language="javascript" >
 function ValidPage() 
    {                             
       if (typeof(Page_ClientValidate) == 'function')  
       { 
            if (typeof (Page_ClientValidate) == 'function') { Page_ClientValidate();     }  
            if (!Page_IsValid) 
            {           

                return false;
            }                  
            return Page_IsValid; 
      } 
      return true;  
    } 

</script>
</head>
<body >
    <form id="form1" runat="server"  onsubmit="return ValidPage();"  >

        <div> 
            <asp:DropDownList ID="ContactPreferences" runat="server"       AutoPostBack="true"  CausesValidation="false"> 
                <asp:ListItem Text="Select" Value="" Selected="True"></asp:ListItem>  
                <asp:ListItem Text="Email" Value="Email" ></asp:ListItem> 
                <asp:ListItem Text="Phone" Value="Phone"></asp:ListItem> 
            </asp:DropDownList> 


            <asp:RequiredFieldValidator    ID="RequiredFieldValidator1" runat="server" ControlToValidate="ContactPreferences" 
            Display="Dynamic" ErrorMessage="Please choose your contact method" 
            EnableClientScript="true" InitialValue=""></asp:RequiredFieldValidator> 

        </div> 

    </form>
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title> 
<script type="text/javascript" language="javascript" >
 function ValidPage() 
    {                             
       if (typeof(Page_ClientValidate) == 'function')  
       { 
            if (typeof (Page_ClientValidate) == 'function') { Page_ClientValidate();     }  
            if (!Page_IsValid) 
            {           

                return false;
            }                  
            return Page_IsValid; 
      } 
      return true;  
    } 

</script>
</head>
<body >
    <form id="form1" runat="server"  onsubmit="return ValidPage();"  >

        <div> 
            <asp:DropDownList ID="ContactPreferences" runat="server"       AutoPostBack="true"  CausesValidation="false"> 
                <asp:ListItem Text="Select" Value="" Selected="True"></asp:ListItem>  
                <asp:ListItem Text="Email" Value="Email" ></asp:ListItem> 
                <asp:ListItem Text="Phone" Value="Phone"></asp:ListItem> 
            </asp:DropDownList> 


            <asp:RequiredFieldValidator    ID="RequiredFieldValidator1" runat="server" ControlToValidate="ContactPreferences" 
            Display="Dynamic" ErrorMessage="Please choose your contact method" 
            EnableClientScript="true" InitialValue=""></asp:RequiredFieldValidator> 

        </div> 

    </form>
</body>
</html>
深府石板幽径 2024-10-13 21:09:53

您是否在 HTML 元素的 onchange 事件中看到 WebForm_DoPostBackWithOptions 方法调用,或者 __doPostBack 方法调用?前者在执行回发之前调用Page_ClientValidate(),您可以使用JS调试工具查看其所采取的路径。另外,由于它是默认验证组,是否还有其他东西会触发它?

Do you see a WebForm_DoPostBackWithOptions method call in the onchange event of the HTML element, or a __doPostBack method call? The former makes a call to Page_ClientValidate() before performing the postback, you could use a JS debugging tool to see the path its taking. Also, since its the default validation group, could something else be triggering it?

情深缘浅 2024-10-13 21:09:53

不知何故,验证器在这里混淆了一些东西。为了防止这种行为,有不同的方法:

1)您可以

EnableClientScript="false"

在验证器上设置,这意味着它在服务器上进行验证。

如果这会产生不需要的副作用(因为验证器被其他客户端验证器“超越”),您可以执行以下操作

2)将此 javascript/jquery 函数添加到页面:

function HideValidator() {

     var validator = $('#<%= ContactMethodRequired.ClientID %>');
     validator.hide();
}

并将事件处理程序添加到 ddl:

onchange="HideValidator();"

Somehow, the validator is confusing something here. To prevent the behaviour, there are different ways:

1) You can set

EnableClientScript="false"

on the validator, meaning it validates on the server.

If this has undesired side effects (because the validator is "overtaken" by other client validators), you can do this

2) add this javascript/jquery-function to the page:

function HideValidator() {

     var validator = $('#<%= ContactMethodRequired.ClientID %>');
     validator.hide();
}

and an event-handler to the ddl:

onchange="HideValidator();"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文