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-buttonlist, 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. How can i achieve this validation based on the condition
Thanks & Regards
Vishwas
3 Replies
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Satyapriya NayakPosted Feb 15, 2013, 7:46 AM
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;
}
}
void clear()
{
TextBox1.Text = "";
txtEmail.Text = "";
txtName.Text = "";
rbtRole.ClearSelection();
}
}
}
Dorababu MekaPosted Feb 15, 2013, 7:14 AM
If you are showing text box on each and every list do this
Dorababu MekaPosted Feb 15, 2013, 6:59 AM