ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.Show Hide RequiredFieldValidator based on condition in ASP.Net using C# and VB.Net<p>Hey <a class="username" rel="dorsa"> dorsa</a>,</p> <p>Please refer below sample.</p> <p>Use visible true and false properties of RequiredFieldValidator.</p> <p><span style="text-decoration: underline;"><strong>HTML</strong></span></p> <pre class="brush: html">&lt;table&gt; &lt;tr&gt; &lt;td&gt; &lt;asp:DropDownList ID=&#34;ListWeekDay3&#34; runat=&#34;server&#34; ValidationGroup=&#34;Validate1&#34; Visible=&#34;False&#34; Width=&#34;94px&#34; Height=&#34;26px&#34; TabIndex=&#34;7&#34;&gt; &lt;asp:ListItem&gt;&lt;/asp:ListItem&gt; &lt;/asp:DropDownList&gt; &lt;asp:RequiredFieldValidator ID=&#34;RequiredFieldValidator11&#34; runat=&#34;server&#34; ControlToValidate=&#34;ListWeekDay3&#34; ErrorMessage=&#34;*&#34; ForeColor=&#34;Red&#34; ValidationGroup=&#34;Validate1&#34;&gt;&lt;/asp:RequiredFieldValidator&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt; &lt;asp:DropDownList ID=&#34;ListWeekDay2&#34; runat=&#34;server&#34; ValidationGroup=&#34;Validate1&#34; Visible=&#34;False&#34; Width=&#34;94px&#34; Height=&#34;26px&#34; TabIndex=&#34;7&#34;&gt; &lt;asp:ListItem&gt;&lt;/asp:ListItem&gt; &lt;/asp:DropDownList&gt; &lt;asp:RequiredFieldValidator ID=&#34;RequiredFieldValidator10&#34; runat=&#34;server&#34; ControlToValidate=&#34;ListWeekDay2&#34; ErrorMessage=&#34;*&#34; ForeColor=&#34;Red&#34; ValidationGroup=&#34;Validate1&#34;&gt;&lt;/asp:RequiredFieldValidator&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt; &lt;asp:RequiredFieldValidator ID=&#34;RequiredFieldValidator4&#34; runat=&#34;server&#34; ControlToValidate=&#34;ListWeekDay1&#34; ErrorMessage=&#34;*&#34; ForeColor=&#34;Red&#34; ValidationGroup=&#34;Validate1&#34;&gt;&lt;/asp:RequiredFieldValidator&gt; &lt;asp:DropDownList ID=&#34;ListWeekDay1&#34; runat=&#34;server&#34; ValidationGroup=&#34;Validate1&#34; Visible=&#34;False&#34; CssClass=&#34;auto-style5&#34; Width=&#34;94px&#34; Height=&#34;26px&#34; TabIndex=&#34;7&#34;&gt; &lt;asp:ListItem&gt;&lt;/asp:ListItem&gt; &lt;/asp:DropDownList&gt; &lt;/td&gt; &lt;asp:TextBox ID=&#34;TextBox1&#34; runat=&#34;server&#34;&gt;&lt;/asp:TextBox&gt; &lt;asp:Button ID=&#34;Button1&#34; runat=&#34;server&#34; OnClick=&#34;Button1_Click&#34; Text=&#34;Submit&#34; ValidationGroup=&#34;Validate1&#34; /&gt; &lt;/tr&gt; &lt;/table&gt;</pre> <p><span style="text-decoration: underline;"><strong>Code</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">protected void Button1_Click(object sender, EventArgs e) { if (TextBox1.Text == &#34;1&#34;) { ListWeekDay1.Visible = true; RequiredFieldValidator11.Visible = true; RequiredFieldValidator11.Visible = false; RequiredFieldValidator4.Visible = false; } else if (TextBox1.Text == &#34;2&#34;) { ListWeekDay1.Visible = true; ListWeekDay2.Visible = true; RequiredFieldValidator11.Visible = false; RequiredFieldValidator10.Visible = true; RequiredFieldValidator4.Visible = true; } else if (TextBox1.Text == &#34;3&#34;) { 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; } }</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) If TextBox1.Text = &#34;1&#34; Then ListWeekDay1.Visible = True RequiredFieldValidator11.Visible = True RequiredFieldValidator11.Visible = False RequiredFieldValidator4.Visible = False ElseIf TextBox1.Text = &#34;2&#34; Then ListWeekDay1.Visible = True ListWeekDay2.Visible = True RequiredFieldValidator11.Visible = False RequiredFieldValidator10.Visible = True RequiredFieldValidator4.Visible = True ElseIf TextBox1.Text = &#34;3&#34; 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</pre> <p>&nbsp;</p>https://www.aspforums.net:443/Threads/103042/Show-Hide-RequiredFieldValidator-based-on-condition-in-ASPNet-using-C-and-VBNet/https://www.aspforums.net:443/Threads/103042/Show-Hide-RequiredFieldValidator-based-on-condition-in-ASPNet-using-C-and-VBNet/Tue, 01 Jan 2019 05:54:18 GMT