C# 从 cs 页面启用/禁用必填字段验证器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15827578/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 18:05:47  来源:igfitidea点击:

Enable/Disable Required field validator from cs page?

c#javascriptasp.netvalidation

提问by Rahul Chowdhury

I have two TextBox and two Buttons in my page.

我的页面中有两个 TextBox 和两个按钮。

One is hidden and the other one is displayed.

一个是隐藏的,另一个是显示的。

When I click the Button1, it will save data of the two TextBoxand will validate each TextBox by the RequiredFieldValidator.

当我单击 时Button1,它将保存两者的数据TextBox并通过RequiredFieldValidator.

Then when I click Button2, it will just hide itself (Button2) and will show the hidden TextBox.

然后当我点击时Button2,它会隐藏自己 ( Button2) 并显示隐藏的TextBox.

Both TextBoxhas RequiredFieldValidatorvalidating against Button1's Event click.

两者TextBoxRequiredFieldValidator针对Button1的事件点击进行了验证。

Now my issue is when I simply enter text to the 1st TextBox and click save, the button click is validating the required field for hidden field. I just want to validate the 2 textbox when it is showing.

现在我的问题是当我简单地将文本输入到第一个文本框并单击保存时,单击按钮正在验证隐藏字段的必填字段。我只想在显示时验证 2 文本框。

How can I avoid this?

我怎样才能避免这种情况?

采纳答案by Praveen Nambiar

Well you can simple use the Enabled="false"property of RequiredFieldValidator.

那么你可以简单使用Enabled="false"的性能RequiredFieldValidator

Your markupwould look something like this based on your Question.

markup根据您的问题,您会看起来像这样。

<asp:TextBox runat="server" ID="tb1"></asp:TextBox>
<asp:RequiredFieldValidator runat="server" ID="rfv1" ControlToValidate="tb1" ErrorMessage="*" ValidationGroup="gvSave">
</asp:RequiredFieldValidator>

<asp:TextBox runat="server" ID="tb2" Visible="false"></asp:TextBox>
<asp:RequiredFieldValidator runat="server" ID="rfv2" ControlToValidate="tb2" ErrorMessage="*" Enabled="false" ValidationGroup="gvSave">
</asp:RequiredFieldValidator>

<asp:Button runat="server" ID="btn1" Text="Save" onclick="btn1_Click" ValidationGroup="gvSave"/>
<asp:Button runat="server" ID="btn2" Text="Show" onclick="btn2_Click" />

And your codebehindlike this:

而你codebehind是这样的:

protected void btn2_Click(object sender, EventArgs e)
{
    tb2.Visible = true;
    rfv2.Enabled = true; // Enables the second requiredfieldvalidator
}

protected void btn1_Click(object sender, EventArgs e)
{
  // your Saving code here
}

回答by Vijay Singh Rana

use the ValidationGroup="group"property to buttonand assign validation group to text on which you want to validate. Hope it will help

使用ValidationGroup="group"属性 tobutton并将验证组分配给要验证的文本。希望它会有所帮助

回答by Grant Thomas

You can specify CausesValidation="false"for the secondary button, this is less verbose and potentially confusing when validation groups are A) excessive for a single field and B) you have to maintain validation groups when adding further controls (do we put it on the button, the validator, the field andthe validation summary? It's not a lot the remember the standard, but less practical when editing.

您可以指定CausesValidation="false"辅助按钮,当验证组 A) 对于单个字段和 B) 您必须在添加更多控件时维护验证组(我们是否将它放在按钮上,验证器,字段验证摘要?记住标准的不是很多,但在编辑时不太实用。

回答by Chetan Sanghani

This is Aspx :

这是 Aspx :

                 <td align="right">
                            Cut Type :
                        </td>
                        <td class="required">
                            <telerik:RadComboBox ID="cmbCutType" runat="server" MaxHeight="200px" Width="200px"
                                Filter="Contains" EnableLoadOnDemand="true" EmptyMessage="Select Cut Type" OnSelectedIndexChanged="cmbCutType_SelectedIndexChanged"
                                AutoPostBack="true">
                            </telerik:RadComboBox>
                            <asp:RequiredFieldValidator runat="server" ID="rfvCutType" Display="None" ControlToValidate="cmbCutType" InitialValue=""
                                ValidationGroup="Save" ErrorMessage="Cut Type is Mandatory"
                                ForeColor="Red"></asp:RequiredFieldValidator>
                            <ajaxToolkit:ValidatorCalloutExtender ID="vceCutType" TargetControlID="rfvCutType"
                                runat="server">
                            </ajaxToolkit:ValidatorCalloutExtender>
                        </td>

This is code behind :

这是后面的代码:

protected void btn2_Click(object sender, EventArgs e)
{
    rfvCutType.IsValid = false;
}

try this.......

尝试这个.......