Required Field Validator Use

G

Guest

Is it possible to use a required field validator and have it make a user
answer a comment field based on a drop down value picked? For instance, if
the user selects "It was terrible", make them have to enter comments before
allowing that page to be submitted.

If that cannot be done, what is the best way to do this. What makes it
harder is that the questions are being placed on the form from a database
dynamically, so we cannot write any java code ahead of time.

Any replies would be greatly appreciated.
 
G

Guest

1)Enable your DropDownList Control for AutoPostBack
2)Implement SelectedIndexChanged Event so thet you Enable or Disable your
Required Field Validator according to the value selected from the DropDownList
for example:

if(cmb.SelectedValue =="YourValue")
{
RequiredFieldValidator1.Enabled = true;
}
else
{
RequiredFieldValidator1.Enabled = false;

}
--
Muhammad Mosa
Software Engineer & Solution Developer
MCT/MCSD.NET
MCTS: .Net 2.0 Web Applications
MCTS: .Net 2.0 Windows Applications
 
G

Guest

Thanks...I will look at your solution.

I am actually hoping to do this client side. I believe I have a good
solution I am coding. I will add an attribute to the dropdown that runs a
java script that takes in the dropdown object, the object that needs
validation, and the values of the dropdown that will cause the validation to
be enabled. Then I will check to see if the dropdown value selected is one
that I will be looking for and then validate the object then. I will then
attempt to attach this to the page submit event.

Thanks for your response though...I did not think of having the dropdown
post back.
 
K

Kumar Reddi

You should use CustomValidator for this purpose. A RequiredFieldValidator
will not work on conditional basis. That is how it was in .net 1.1.. I am
not sure if it has changed the way it works in asp.net 2.0
 
Joined
Feb 2, 2009
Messages
1
Reaction score
0
Validate dropdownlists using required field validators

Yes is it possible to set a required field validator for a dropdownlist. But you need to do the following things:

a. There must be a default value in the dropdownlist viz:
Text: (Select an Item)
Value: 0

b. Set required field value property, InitialValue="0".

Here's a proper example:

Code:
<asp:DropDownList ID="DropDownListItemTypes" runat="server">
                        <asp:ListItem Text="Select an Item" Value="0"></asp:ListItem>
                        <asp:ListItem Text="Item1" Value="1"></asp:ListItem>
                        <asp:ListItem Text="Item2" Value="2"></asp:ListItem>
                    </asp:DropDownList>
                    <asp:RequiredFieldValidator ID="rfvDropDownListItemTypes" ControlToValidate="DropDownListItemTypes"
                        runat="server" ErrorMessage="*" InitialValue="0"></asp:RequiredFieldValidator>


Hope, this information was useful. :)

Regards,

Chaitanya Phadke
 
Last edited:

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,739
Latest member
Clint8040

Latest Threads

Top