• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 918
  • Last Modified:

Validating a custom UserControl via standard validation controls such as RequiredFieldValidator, CustomValidator, etc.

I am a beginner at ASP.NET and have just created my first UserControl which simply contains a drop-down list that is populated from the database.  This works well for me since I want to create multiple instances of this functionality.  

However, I run into problems when I want to validate my control, on the client side, the way I would validate a standard drop-down list control (one that was not encapsulated in a UserControl).  I've considered putting the validator inside my control but I don't want my error message to appear right next to the drop-down list.  

The first selection in the drop-down list is always something like "-- Select a Category --" or something like that and I would like to ensure that the user makes a selection other than the first entry with a validator like this:

<asp:CompareValidator ID="userControlValidator"
                                                   runat="server"
                                                   ErrorMessage="Please select a category."
                                                   ControlToValidate="customCategoryUserControl"
                                                   ValueToCompare="0"
                                                   Operator="NotEqual" />

Is there a way to do something like this which:
1.  Allows the validator to live on the same page as the UserControl (not in the user control itself) and
2.  Makes validation happen on the client

Thanks!

0
moshecristel
Asked:
moshecristel
  • 3
  • 3
  • 2
1 Solution
 
DropZoneCommented:
You may want to try implementing a Server Control instead of a UserControl.  It may be a bit more complicated, but its definitely worth it:  you'll have a lot more control over how it works and renders.

     -dZ.
0
 
moshecristelAuthor Commented:
Are you saying that, instead, I should extend the standard DropDownList control?  If so, how do I go about doing this?  I will do some research but wondering if you could point me in the right direction.

Thanks!
0
 
DropZoneCommented:
Yes, that's what I am saying.  What you could do is descend from the DropDownList control, and override the OnDataBinding() event.  In there, perform your database connection and set the DataSource property prior to calling the base method, which will then DataBind the control with the data you retrieve.  Its not that hard really.

There's plenty more things you can do with a server control, but for your particular case, that's all you need to do.

    -dZ.
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
moshecristelAuthor Commented:
I've created my extended control but am having trouble including it my page.  From what I'm reading I have to compile it into a .dll and include the assembly when I register the control.  Is there any way for me to including it without compilation--the way that you do with user controls?

If not, is this something you have to do from the command line every time you make a change to the control?  I have Visual Studio .NET 2003 but there doesn't seem to be an easy way to do this from the interface.
0
 
RejojohnyCommented:
you do not need to compile it into a dll, everytime i.e if you are in still developing .. create the control on a separate project and have another project for testing .. have both the project in one solution .. in the test project, add the control's project as a reference (project reference) .. so everytome the control project is changed, the test project will automatically have the latest reference. ..

Rejo
0
 
RejojohnyCommented:
by the way, you could also try updating the controltovalidate property of the validator control dyamically after the user control is loaded .. you could probably use page.findcontrol to find the control withn ur user control .. just a thought ..
0
 
moshecristelAuthor Commented:
Thanks for the help.  It seems like extending the server control is just an all-around better practice for the type of thing I am doing.

Really appreciate the help!
0
 
DropZoneCommented:
Yes it is.  UserControls are mainly for the simplest of things.  In fact, I'll venture to say that it is always better to extend a server control than to create a user control; even performance is better.

   -dZ.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now