Click here to Skip to main content
11,410,118 members (61,027 online)
Rate this: bad
good
Please Sign up or sign in to vote.
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 20-Mar-13 18:28pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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 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..
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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,
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

but doesn't that caused other validation to also disabled?

i'll give it a try first
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

i already found the solution, it works for me though

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


thanks all
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 169
1 OriginalGriff 70
2 BillWoodruff 70
3 Maciej Los 68
4 Philippe Mori 60
0 Sergey Alexandrovich Kryukov 8,755
1 OriginalGriff 6,915
2 Maciej Los 3,390
3 Abhinav S 3,248
4 Peter Leow 3,059


Advertise | Privacy | Mobile
Web03 | 2.8.150414.5 | Last Updated 21 Mar 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100