[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2007-08-05
8
Medium Priority
?
915 Views
Last Modified: 2013-11-05
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
Comment
Question by:moshecristel
  • 3
  • 3
  • 2
8 Comments
 
LVL 18

Accepted Solution

by:
DropZone earned 1000 total points
ID: 19635870
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
 

Author Comment

by:moshecristel
ID: 19635918
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
 
LVL 18

Expert Comment

by:DropZone
ID: 19635996
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:moshecristel
ID: 19636039
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
 
LVL 26

Expert Comment

by:Rejojohny
ID: 19636307
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
 
LVL 26

Expert Comment

by:Rejojohny
ID: 19636319
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
 

Author Comment

by:moshecristel
ID: 19637793
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
 
LVL 18

Expert Comment

by:DropZone
ID: 19638265
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

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Learn how to set-up custom confirmation messages to users who complete your Wufoo form. Include inputs from fields in your form, webpage redirects, and more with Wufoo’s confirmation options.
Learn how to set-up PayPal payment integration in your Wufoo form. Allow your users to remit payment through PayPal upon completion of your online form. This is helpful for collecting membership payments, customer payments, donations, and more.
Suggested Courses
Course of the Month19 days, 10 hours left to enroll

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question