Click here to Skip to main content
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 OriginalGriff 195
1 Afzaal Ahmad Zeeshan 152
2 ProgramFOX 130
3 Maciej Los 130
4 den2k88 80
0 OriginalGriff 6,564
1 Sergey Alexandrovich Kryukov 6,048
2 DamithSL 5,228
3 Manas Bhardwaj 4,657
4 Maciej Los 4,150


Advertise | Privacy | Mobile
Web04 | 2.8.1411022.1 | Last Updated 21 Mar 2013
Copyright © CodeProject, 1999-2014
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