Last post Dec 16, 2016 06:10 AM by anuj_koundal
Dec 15, 2016 08:36 PM|bootzilla|LINK
I have a dropdown(code is name of the dropdown) that when coming from another page it prefills with a value, the required validator fires off. I have a select statement that fills in all textboxes and dropdowns on this page. When I open this page by itself
the validation functions as normal meaning when I select a value from the dropdown no validation and when it is blank it validates. Here is the code for the select statement and reading in the data on page load:
OracleConnection conn = new OracleConnection();
OracleCommand cmd = new OracleCommand();
conn.ConnectionString = strConnection;
cmd.Connection = conn;
cmd.CommandText = "Select RID, CODE, CUSTOMER_NAME from ACTIVITY WHERE ID = :IDValue";
//Add values to paramter
cmd.Parameters.Add(new OracleParameter("IDValue", Request.QueryString["ID"]));
OracleDataAdapter da = new OracleDataAdapter(cmd);
cmd.CommandType = CommandType.Text;
OracleDataReader dr = cmd.ExecuteReader();
name.Text = dr["customer_name"].ToString();
code.SelectedItem.Text = dr["code"].ToString();
and here is the image of the validator with value for dropdown filled. I know it's more fields on this image then in code but I didn't include all of the code for this post and only the one that is the problem.
Dec 16, 2016 04:48 AM|anuj_koundal|LINK
Hi Bootzilla you need to add extra item in your dropdownlist to validate using required field validator.
Dec 16, 2016 05:09 AM|bootzilla|LINK
Based off of that example I'm not sure what to add. Is it AutoPostBack?, Display=Dynamic?, InitialValue = -1? What am I adding? There are a lot of things discussed on that page.
Dec 16, 2016 06:10 AM|anuj_koundal|LINK
Use this code to add extra item at the top of dropdownlist:
//==== Add select option.
ddl.Items.Insert(0, new ListItem("Select", "0"));
This will insert a new item in your dropdown having Text="Select" and value="0"
Now modify your required field validator:
<asp:RequiredFieldValidator ErrorMessage="Please select item" ValidationGroup="yourValGroup" ControlToValidate="idOfDropdownlist" InitialValue="0" runat="server" />
This will show error message if first option(The item we have inserted manually) is selected.