Using RequiredFieldValidator with DropdownList Asp.Net


You may have used RequiredFieldValidator with Text controls, but if you wnat to use it with DropDownList controls, you need to do some little extra work. Here is the trick.

Step 1: Create an DropDownList

Step 2: Drag and RequiredFieldValidator. Set it’s ControlToValidate property to the DropDownLis’s object.

Step 3: Fill some items in the DropDownList. Add a first item as “Select”. This will indicate the user that he should select an option.

Step 4: Now set the RequiredFieldValidator’s “InitialValue” property to “Select”.

Now all of your code should look like this :

<asp:DropDownList ID=”cmbCountry” runat=”server”>
<asp:ListItem>Select</asp:ListItem>
<asp:ListItem>India</asp:ListItem>
<asp:ListItem>USA</asp:ListItem>
<asp:ListItem>Canada</asp:ListItem>
</asp:DropDownList>

<asp:RequiredFieldValidator ID=”rqCountry” runat=”server” ErrorMessage=”*” SetFocusOnError=”True” ControlToValidate=”cmbCountry” InitialValue=”Select”></asp:RequiredFieldValidator>

Now you are all set  to go and test it.

To read complete post and source code please visit dotnetcoderoom.blogspot.com

Advertisements

About dotnetcoderoom

Working as an Onsite coordinator in USA. Handling UAT, CR (Change Requests), Product Support/Development, Build Management. Supervise and assist in live product implementation (at client side) and training. Providing technical direction for the development, design, and system integration. Closely monitoring and working with offshore team for Enhancements, Bug Fixing, Enforce coding standards, perform code reviews and mentors junior developers. Proven track of timely delivery of enterprise level web based applications. Skilled in a variety of software languages, tools, and methodologies, with a special expertise in Microsoft .NET technologies. MCTS .Net 4.0 Web applications MCTS .Net 2.0 Web applications GNIIT, MCA, B.Com Specialties 8+ Years of experience in .NET Technologies. Expertise in .NET, ASP.NET, MVC, C#, VB.NET, WCF, Web Services, MS SQL, JQuery. Excellent R&D and troubleshooting skills. Extensive knowledge in E-Commerce and Healthcare domain. View all posts by dotnetcoderoom

2 responses to “Using RequiredFieldValidator with DropdownList Asp.Net

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: