I have a scenario, where user has to select any one role from radiobuttonlist to register himself as below
- Administrator
- Shop floor User
- Key User
- Others
If he selects Role "Others" from radio-button list, a text box will be appeared to enter the role manually. Can I have a RequiredFieldValidator for text-box if only user selects role "Others" else if he selects other roles validation is not required.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Required_Field_Validator_based._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html
xmlns="http://www.w3.org/1999/xhtml"
>
<head
runat="server">
<title>Untitled
Page</title>
</head>
<body>
<form
id="form1"
runat="server">
<div>
<table
align="center">
<tr>
<td
colspan="2">
<h3>
Registration Form</h3>
</td>
</tr>
<tr>
<td>
Name:</td>
<td>
<asp:TextBox
ID="txtName"
runat="server"></asp:TextBox>
</td>
<td><asp:RequiredFieldValidator
ID="RequiredFieldValidator2"
runat="server"
ErrorMessage="Enter
Name" ControlToValidate="txtName"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
Email</td>
<td>
<asp:TextBox
ID="txtEmail"
runat="server"></asp:TextBox>
</td>
<td><asp:RequiredFieldValidator
ID="RequiredFieldValidator3"
runat="server"
ErrorMessage="Enter
Email" ControlToValidate="txtEmail"></asp:RequiredFieldValidator>
</td>
<td>
<asp:RegularExpressionValidator
ID="RegularExpressionValidator1"
runat="server"
ErrorMessage="Invaid
Email" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"
ControlToValidate="txtEmail"></asp:RegularExpressionValidator>
</td>
</tr>
<tr>
<td>
Role</td>
<td>
<asp:RadioButtonList
ID="rbtRole"
runat="server"
RepeatDirection="Vertical"
AutoPostBack="True"
onselectedindexchanged="rbtRole_SelectedIndexChanged">
<asp:ListItem>Administrator</asp:ListItem>
<asp:ListItem>Shop
floor User</asp:ListItem>
<asp:ListItem>Key
User</asp:ListItem>
<asp:ListItem>Others</asp:ListItem>
</asp:RadioButtonList>
</td>
<td><asp:RequiredFieldValidator
ID="RequiredFieldValidator4"
runat="server"
ErrorMessage="Select
role" ControlToValidate="rbtRole"></asp:RequiredFieldValidator>
</td>
</tr>
<asp:Panel
ID="panell"
runat="server"
Visible="false">
<tr>
<td></td><td><asp:TextBox
ID="TextBox1"
runat="server"></asp:TextBox></td>
<td><asp:RequiredFieldValidator
ID="RequiredFieldValidator1"
runat="server"
ErrorMessage="Enter
role" ControlToValidate="TextBox1"></asp:RequiredFieldValidator>
</td>
</tr>
</asp:Panel>
<tr>
<td
align="center"
colspan="2">
<asp:Button
ID="btn_register"
runat="server"
Text="Register"
onclick="btn_register_Click"
/>
</td>
</tr>
<tr>
<td
align="center"
colspan="2">
<asp:Label
ID="lblmsg"
runat="server"></asp:Label>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
using System;
using
System.Collections;
using
System.Configuration;
using
System.Data;
using
System.Linq;
using
System.Web;
using
System.Web.Security;
using
System.Web.UI;
using
System.Web.UI.HtmlControls;
using
System.Web.UI.WebControls;
using
System.Web.UI.WebControls.WebParts;
using
System.Xml.Linq;
using
System.Data.SqlClient;
namespace
Required_Field_Validator_based
{
public partial
class _Default
: System.Web.UI.Page
{
string strConnString =
ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlCommand com;
protected void
btn_register_Click(object sender, EventArgs e)
{
if (rbtRole.Text ==
"Others")
{
SqlConnection con =
new SqlConnection(strConnString);
com = new
SqlCommand();
com.Connection = con;
com.CommandType = CommandType.Text;
com.CommandText = "insert into login
values(@Name,@Email,@role)";
com.Parameters.Clear();
com.Parameters.AddWithValue("@Name",
txtName.Text);
com.Parameters.AddWithValue("@Email",
txtEmail.Text);
com.Parameters.AddWithValue("@role",
TextBox1.Text);
if (con.State ==
ConnectionState.Closed)
con.Open();
com.ExecuteNonQuery();
con.Close();
lblmsg.Text = "Data entered
successfully!!!";
clear();
}
else
{
SqlConnection con =
new SqlConnection(strConnString);
com = new
SqlCommand();
com.Connection = con;
com.CommandType = CommandType.Text;
com.CommandText = "insert into login
values(@Name,@Email,@role)";
com.Parameters.Clear();
com.Parameters.AddWithValue("@Name",
txtName.Text);
com.Parameters.AddWithValue("@Email",
txtEmail.Text);
com.Parameters.AddWithValue("@role",
rbtRole.SelectedValue);
if (con.State ==
ConnectionState.Closed)
con.Open();
com.ExecuteNonQuery();
con.Close();
lblmsg.Text = "Data entered
successfully!!!";
clear();
}
}
protected void
rbtRole_SelectedIndexChanged(object sender,
EventArgs e)
{
lblmsg.Text = "";
if (rbtRole.SelectedValue ==
"Others")
{
panell.Visible = true;
TextBox1.CausesValidation = true;
}
else
{
panell.Visible = false;
TextBox1.CausesValidation = false;
}
}
private void
clear()
{
TextBox1.Text = "";
txtEmail.Text = "";
txtName.Text = "";
rbtRole.ClearSelection();
}
}
}

Dorababu MekaPosted Feb 16, 2013, 12:57 AM
Hi Satyapriya Nayak I think CausesValidation =false will not stop validation firing as you make your panel visible to false may be you didn't noticed that. Try by making the panel visible to true and hit enter in the text box with out entering any text in the textbox you can see validation fires on both. Instead of that you have to make your validation enabled to true or false RequiredFieldValidator1.Enabled = true; on others and RequiredFieldValidator1.Enabled = false; for the remaining