vb.net 验证两个控件 (CustomValidator)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16111763/
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-09-17 13:20:11  来源:igfitidea点击:

Validate two controls (CustomValidator)

c#asp.netvb.net

提问by user1187282

Before submitting the form I need to test if the sum ( txtA + txtB) is greater than 100. Is it possible to do this with a CustomValidator, because I don't know if I can choose the 2 textbox in controltovalidate

在提交表单之前,我需要测试总和(txtA + txtB)是否大于 100。是否可以使用 a 来执行此操作CustomValidator,因为我不知道是否可以在 controltovalidate 中选择 2 个文本框

<asp:TextBox ID="txtA" runat="server"></asp:TextBox>
<asp:TextBox ID="txtB" runat="server"></asp:TextBox>
<asp:CustomValidator ID="CustomValidator2" 
                     runat="server" 
                     ErrorMessage="CustomValidator" />

<asp:Button ID="Button1" runat="server" Text="Button" />

Thanks.

谢谢。

回答by Arshad

you can do as :

你可以这样做:

<asp:TextBox ID="txtA" runat="server" /> 
<asp:TextBox ID="txtB" runat="server" />
<asp:CustomValidator ID="CV1"runat="server" 
    OnServerValidate="ServerValidation" 
    ErrorMessage="Sum is less than 100" />

codebehind :

代码隐藏:

protected void ServerValidation(object source, ServerValidateEventArgs args)
{
    args.IsValid = int.Parse(txtA.Text)+ int.Parse(txtB.Text) >100;
}

回答by Zinov

When you drop a custom validation in your page, you can link the validator to a control, but if you want to perform multiple validations over more than one control, you need to include the following attribute

当您在页面中放置自定义验证时,您可以将验证器链接到一个控件,但是如果您想对多个控件执行多个验证,则需要包含以下属性

 OnServerValidate="MyMethodOnServerSide" 

and define that method on the server side

并在服务器端定义该方法

protected void MyMethodOnServerSide(object source, ServerValidateEventArgs args)
{
     if (string.IsNullOrEmpty(mytxt1.Text) &&
            string.IsNullOrEmpty(mytxt2.Text))
            {
                args.IsValid = false;
                return;
            }

            args.IsValid = true;
}

just asign the args.IsValidproperty to the value you need. On the other hand the validation is done before you load the page, so if you clicked a button that performs an action like reading values from the DB in case everything is correct, on that action you need to include the following check.

只需将args.IsValid属性分配给您需要的值。另一方面,验证是在加载页面之前完成的,因此如果您单击了一个按钮来执行诸如从数据库读取值之类的操作,以防万一一切正确,您需要在该操作中包含以下检查。

protected void cmdSearch_Click(object sender, EventArgs e)
{
    if (Page.IsValid)
    {
          LoadDataFromDB();
    }
}

When args.IsValidis false then Page.IsValidis false too. Hope this helps

args.IsValid是假的时候Page.IsValid也是假的。希望这可以帮助

回答by Mike Perrenoud

You need to add another control, <asp:HiddenField>and then leverage jQueryto set the value of that control. It might look something like this:

您需要添加另一个控件,<asp:HiddenField>然后利用jQuery来设置该控件的值。它可能看起来像这样:

MARKUP

标记

<asp:HiddenField ID="SumOfValues" />
<asp:CustomValidator ID="CustomValidator2"
    runat="server"
    ErrorMessage="CustomValidator"
    ControlToValidate="SumOfValues" />

JQUERY

查询

$(document).ready(function() {
    $('#txtA').change(sumValues);
    $('#txtB').change(sumValues);
});

function sumValues() {
    var val1 = $('txtA').value();
    if (val1 === undefined) { val1 = 0; }

    var val2 = $('txtB').value();
    if (val2 === undefined) { val2 = 0; }

    $('#SumOfValues').value(val1 + val2);
}

and that should allow you validate that hidden control. However, one thing you'll need to make sure to do on all three controls is leverage ClientIDModeand set it to Staticso that the names are exactly what you specify in the markup when they get to the page.

这应该允许您验证该隐藏控件。但是,您需要确保对所有三个控件执行的一件事是利用ClientIDMode并将其设置为Static使名称与您在到达页面时在标记中指定的名称完全一致。