如何使用自定义验证器取决于下拉值 [英] how to use custom validator depends on dropdown value

查看:95
本文介绍了如何使用自定义验证器取决于下拉值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hi all,

i have come confusion, how can we achieve this goal

for example, i have a dropdown, which the item is HR, Admin, IT

eg : <asp:DropDownList ID="DropDownList22" runat="server" OnSelectedIndexChanged="DropDownList22_SelectedIndexChanged" AutoPostBack="true" >
    <asp:ListItem>IT</asp:ListItem>
    <asp:ListItem>Admin</asp:ListItem>
    <asp:ListItem>HR</asp:ListItem>
</asp:DropDownList>

so, i want to put my validation here in date textbox, only if user select IT, this textbox will validate, for other (Admin and HR) they can left the date empty

<asp:TextBox ID="TextBox32" runat="server" AutoPostBack="true" OnTextChanged="TextBox32_TextChanged"></asp:TextBox>

what kind of validator can i use and how are the validator can be made?
i already try put requiredvalidator, but if i select admin and left the field empty, i got the errormessage too.

<asp:RequiredFieldValidator ID="chkDateReturn" runat="server" ControlToValidate="TextBox32" ErrorMessage="Must select the return date" ></asp:RequiredFieldValidator>

thanks

推荐答案

请尝试以下..



C#代码如下..

Try below..

C# code below..
protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            if (DropDownList22.SelectedItem.Text == "IT")
            {
                Button1.CausesValidation = true;
            }
            else
            {
                Button1.CausesValidation = false;
            }
        }
    }

    protected void DropDownList22_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (DropDownList22.SelectedItem.Text == "IT")
        {
            Button1.CausesValidation = true;
        }
        else
        {
            Button1.CausesValidation = false;
        }
    }



以下HTML代码..


HTML code below..

<asp:DropDownList ID="DropDownList22" runat="server" OnSelectedIndexChanged="DropDownList22_SelectedIndexChanged"

            AutoPostBack="true">
            <asp:ListItem>IT</asp:ListItem>
            <asp:ListItem>Admin</asp:ListItem>
            <asp:ListItem>HR</asp:ListItem>
        </asp:DropDownList>
        <asp:TextBox ID="TextBox32" runat="server" AutoPostBack="true" OnTextChanged="TextBox32_TextChanged"></asp:TextBox>
        <asp:RequiredFieldValidator ID="chkDateReturn" runat="server" ControlToValidate="TextBox32"

            ErrorMessage="Must select the return date"></asp:RequiredFieldValidator>
        <asp:Button ID="Button1" runat="server" Text="Button" CausesValidation="false" />





希望它可以帮助你..



Hope it helps you..


你好,



在你的服务器端页面加载处理程序检查下拉列表的值,如果它除了IT以外的任何内容,然后将TextBox32的 CausesValidation 属性设置为false。您还可以将chkDateReturn的Enabled属性设置为false。



问候,
Hello,

In your server side page load handler check the value of dropdown and if it anything other than IT then set CausesValidation property of the TextBox32 to false. You can also set Enabled property of chkDateReturn to false.

regards,


但不会导致其他验证残疾人?



我先试试吧
but doesn't that caused other validation to also disabled?

i'll give it a try first


这篇关于如何使用自定义验证器取决于下拉值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆