Show Hide RequiredFieldValidator based on condition in ASP.Net using C# and VB.Net

Last Reply 5 months ago By pandeyism

Posted 5 months ago

I have a web form with some text box and some RequiredFieldValidator like Below:

http://uupload.ir/files/u33y_course.png

in my code, I have a variable that name is session. if the session is 1 in my web form just show the highlight controls and another one visible is false. if the session is 2, the visible properties of controls are true and the second column is displayed and if the session is 3 the third one is shown.

Now the problem happens when the session is 1 or 2. after that I want to insert the data of controls to my DB, the inserting do correctly but in my web form I see all of the RequiredFieldValidator like below:

http://uupload.ir/files/7xq_course2.png

How can I fix this problem?

<asp:RequiredFieldValidator ID="RequiredFieldValidator11" runat="server" ControlToValidate="ListWeekDay3"
    ErrorMessage="*" ForeColor="Red" ValidationGroup="Validate1"></asp:RequiredFieldValidator>
<asp:DropDownList ID="ListWeekDay3" runat="server" ValidationGroup="Validate1" Visible="False"
    Width="94px" Height="26px" TabIndex="7">
    <asp:ListItem></asp:ListItem>
</asp:DropDownList>
</td> </tr>
<tr>
    <td>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator10" runat="server" ControlToValidate="ListWeekDay2"
            ErrorMessage="*" ForeColor="Red" ValidationGroup="Validate1"></asp:RequiredFieldValidator>
        <asp:DropDownList ID="ListWeekDay2" runat="server" ValidationGroup="Validate1" Visible="False"
            Width="94px" Height="26px" TabIndex="7">
            <asp:ListItem></asp:ListItem>
        </asp:DropDownList>
    </td>
</tr>
<tr>
    <td>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ControlToValidate="ListWeekDay1"
            ErrorMessage="*" ForeColor="Red" ValidationGroup="Validate1"></asp:RequiredFieldValidator>
        <asp:DropDownList ID="ListWeekDay1" runat="server" ValidationGroup="Validate1" Visible="False"
            CssClass="auto-style5" Width="94px" Height="26px" TabIndex="7">
            <asp:ListItem></asp:ListItem>
        </asp:DropDownList>
    </td>
    <p>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" ValidationGroup="Validate1" />

 

protected void Button1_Click(object sender, EventArgs e)
{
    if (TextBox1.Text == "1")
    {
        ListWeekDay1.Visible = true;
    }
    else if (TextBox1.Text == "2")
    {
        ListWeekDay1.Visible = true;
        ListWeekDay2.Visible = true;
    }
    else if (TextBox1.Text == "3")
    {
        ListWeekDay1.Visible = true;
        ListWeekDay2.Visible = true;
        ListWeekDay3.Visible = true;
    }
    else
    {
        ListWeekDay1.Visible = false;
        ListWeekDay2.Visible = false;
        ListWeekDay3.Visible = false;
    }
}

 

Posted 5 months ago

Hey dorsa,

Please refer below sample.

Use visible true and false properties of RequiredFieldValidator.

HTML

<table>
    <tr>
        <td>
            <asp:DropDownList ID="ListWeekDay3" runat="server" ValidationGroup="Validate1" Visible="False"
                Width="94px" Height="26px" TabIndex="7">
                <asp:ListItem></asp:ListItem>
            </asp:DropDownList>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator11" runat="server" ControlToValidate="ListWeekDay3"
                ErrorMessage="*" ForeColor="Red" ValidationGroup="Validate1"></asp:RequiredFieldValidator>
        </td>
    </tr>
    <tr>
        <td>
            <asp:DropDownList ID="ListWeekDay2" runat="server" ValidationGroup="Validate1" Visible="False"
                Width="94px" Height="26px" TabIndex="7">
                <asp:ListItem></asp:ListItem>
            </asp:DropDownList>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator10" runat="server" ControlToValidate="ListWeekDay2"
                ErrorMessage="*" ForeColor="Red" ValidationGroup="Validate1"></asp:RequiredFieldValidator>
        </td>
    </tr>
    <tr>
        <td>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ControlToValidate="ListWeekDay1"
                ErrorMessage="*" ForeColor="Red" ValidationGroup="Validate1"></asp:RequiredFieldValidator>
            <asp:DropDownList ID="ListWeekDay1" runat="server" ValidationGroup="Validate1" Visible="False"
                CssClass="auto-style5" Width="94px" Height="26px" TabIndex="7">
                <asp:ListItem></asp:ListItem>
            </asp:DropDownList>
        </td>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Submit" ValidationGroup="Validate1" />
    </tr>
</table>

Code

C#

protected void Button1_Click(object sender, EventArgs e)
{
    if (TextBox1.Text == "1")
    {
        ListWeekDay1.Visible = true;
        RequiredFieldValidator11.Visible = true;
        RequiredFieldValidator11.Visible = false;
        RequiredFieldValidator4.Visible = false;
    }
    else if (TextBox1.Text == "2")
    {

        ListWeekDay1.Visible = true;
        ListWeekDay2.Visible = true;
        RequiredFieldValidator11.Visible = false;
        RequiredFieldValidator10.Visible = true;
        RequiredFieldValidator4.Visible = true;
    }
    else if (TextBox1.Text == "3")
    {
        ListWeekDay1.Visible = true;
        ListWeekDay2.Visible = true;
        ListWeekDay3.Visible = true;
        RequiredFieldValidator11.Visible = true;
        RequiredFieldValidator10.Visible = true;
        RequiredFieldValidator4.Visible = true;
    }
    else
    {
        ListWeekDay1.Visible = false;
        ListWeekDay2.Visible = false;
        ListWeekDay3.Visible = false;
    }
}

VB.Net

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
    If TextBox1.Text = "1" Then
        ListWeekDay1.Visible = True
        RequiredFieldValidator11.Visible = True
        RequiredFieldValidator11.Visible = False
        RequiredFieldValidator4.Visible = False
    ElseIf TextBox1.Text = "2" Then
        ListWeekDay1.Visible = True
        ListWeekDay2.Visible = True
        RequiredFieldValidator11.Visible = False
        RequiredFieldValidator10.Visible = True
        RequiredFieldValidator4.Visible = True
    ElseIf TextBox1.Text = "3" Then
        ListWeekDay1.Visible = True
        ListWeekDay2.Visible = True
        ListWeekDay3.Visible = True
        RequiredFieldValidator11.Visible = True
        RequiredFieldValidator10.Visible = True
        RequiredFieldValidator4.Visible = True
    Else
        ListWeekDay1.Visible = False
        ListWeekDay2.Visible = False
        ListWeekDay3.Visible = False
    End If
End Sub