Click here to Skip to main content
15,882,017 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
XML
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
Posted
Updated 4-Jun-18 1:37am
v2

Try below..

C# code below..
C#
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 code below..
HTML
<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..
 
Share this answer
 
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,
 
Share this answer
 
but doesn't that caused other validation to also disabled?

i'll give it a try first
 
Share this answer
 
i already found the solution, it works for me though

XML
<%
                    if (DropDownList22.SelectedValue == "IT") {
                %>
                <asp:RequiredFieldValidator ID="chkDept" runat="server" ControlToValidate="TextBox32" ErrorMessage="Must select" ></asp:RequiredFieldValidator>
                <% } %>



thanks all
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900