简体   繁体   English

即使禁用了验证器,Asp:RequiredFieldValidator也会导致在服务器上进行验证

[英]Asp:RequiredFieldValidator causes validation on Server even if validator is disabled

I will keep this as short as possble but its wrecking my head. 我将尽可能简短,但会伤到我的头。 I have a user control with a label and a field. 我有一个带有标签和字段的用户控件。 I add the user control dynamically to a page at run time. 我在运行时将用户控件动态添加到页面中。 There could be many user controls which are built dynamically from data I get from the data base. 可能有许多用户控件是根据我从数据库中获得的数据动态构建的。 The data describes the label name and if the text box should be completed.. ie is it required. 数据描述标签名称以及文本框是否应填写。即是否需要。 I have a submit on the main page which has a validation group.All the validators on the user controls have the same validation group. 我在主页上有一个验证组的提交。用户控件上的所有验证器都具有相同的验证组。 So far so good I hope. 到目前为止,我希望如此。

The user control has a RequiredFieldValidator for the text box and looks all like this. 用户控件的文本框具有RequiredFieldValidator,看起来像这样。 The validator is enabled by default. 默认情况下,验证器处于启用状态。

<asp:TextBox ID="dfControl" runat="server" Text="" ToolTip="ucsAlphanumericEntryDC" Width="95%" CausesValidation="true"/>
<asp:RequiredFieldValidator ID="rfvContol" runat="server" ErrorMessage="Value is required" EnableClientScript="true" 
    ValidationGroup="ValidateContols" ControlToValidate="dfControl" Display="Dynamic" Enabled="true"></asp:RequiredFieldValidator>

When I dynamically load the control I check a parameter to tell me if the field is required or not. 当我动态加载控件时,我检查一个参数来告诉我该字段是否必需。 The paramater is from the database which represents the control that I am about to draw. 参数来自数据库,该数据库代表我将要绘制的控件。 If the Field NOT a required field I disable the validation in the page_preRender.. also tried it in load. 如果该字段不是必填字段,那么我将在page_preRender中禁用验证。

       if (!DisplayRequired)
        {
            rfvContol.EnableClientScript = false;
            rfvContol.Enabled = false;
            dfControl.CausesValidation = false;
        }

So far so good. 到现在为止还挺好。 This all works on the client side validation for only the user controls with the Display Required set to true, leaving the Validator enabled. 所有这些都仅对将“ Display Required”设置为true的用户控件进行客户端验证,而使Validator保持启用状态。

however on posting page from a submit button which I have wired to the same validation group as the user controls, I check the IsValid on the submit and if its false I dont save anything. 但是,在从提交按钮发布的页面上,我已将其连接到与用户控件相同的验证组,我检查了提交中的IsValid,如果它为false,则不保存任何内容。 But when the page re-renders on the client.. I have error messages on all the user controls, saying that they are required... even though I disabled some of them that where not required. 但是,当页面在客户端上重新呈现时。.我在所有用户控件上都显示错误消息,说它们是必需的...即使我禁用了其中一些不需要的信息。 the View state is default on the control which I assume to be on. 我假定已打开控件的默认视图状态。

I also tried not redrawing the controls with the validators, by enclosing the code in a if(!IsPostBack) 我还尝试通过不将代码包含在if(!IsPostBack)中来不重绘带有验证器的控件。

Has anyone any idea's why the validators fire when they are disabled on the first page load but dont get disabled on the postback. 有谁知道为什么验证器在第一页加载时被禁用但在回发时不被禁用时会触发的原因。

thanks Mick 谢谢米克

Try either setting Visible = false from the server, this works for me. 尝试从服务器设置Visible = false ,这对我有用。 Or you can also use the client-side ValidatorEnable(valid, false); 或者,您也可以使用客户端的ValidatorEnable(valid, false); to disable the validator. 禁用验证器。

HTH. HTH。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM