Validation of Custom Controls
来源:http://www.imcoder.org/custom-control/189736.htm
Hi, I have a custom control allowing the user to enter a username and the system to retrieve the user id that correspond with the user name.
The idea now is to have (input)validation in the usercontrol, so that the custom control will show an error when the user enters a non-existing username == no id can be retrieved from the DB on this input.
It would be great that validation can be triggered by calling the Page.IsValid:Validate which also validate normal required fields etc...
I do want the validation and message all in the user control. The control is used on many pages, by different developers who don't/shouldn't know how it works.
So how can I insert this validation in the control?? Interesting to say, the username is created as a combination of a dropdownlist and a textbox. ;)
The idea is to have below it some red text explaining that the user is not found in the db, and so nothing can be done until a correct user is entered.
cheers
Couldn't you use the <asp:CustomValidator> control?
I could but that requires the validation done on the page's code behind. Meaning also that the "user" of the control needs to know more about the control and not forget to do this validation when he uses it. I'm looking for a solution where the validation is done by the control, and just a Property allowing to switch on or off the validation like:
control.Validating = true;
You are going to have a hard time checking a username against a database from a client-side validation script. I don't understand why you don't want to do the validation from the code-behind? And with the custom validator, you can turn it on or off as you are wanting as well.
Oh no, you got me wrong there. no client-side validation script has ever crossed my mind.
I just want to have the validation in the custom control itself, server side. So with the display of errors when not valid implemented in the custom control etc...
I'm sure it's possible and I'm sure I'll find a solution. Probably not before we have our first release, so for now on I'll have to do it through custom validation or so on the page.
But it has to work in the future when more developers will create modules for the site with the same controls.
I cannot expect from the entire company to learn asp.net and custom validation etc... :D
Hallo littlefool
You want the whole validation thing in the control itself?
And the result of the internal validation should also be sensitive to Page.IsValid, right?
You can implement the interface IValidator to your Control and add the control itself to the Page.Validators.
An example of this can be found on my blog.
Its written in german, but i think that shoud not be a problem to understand the code.
- http://www.aspnetzone.de/blogs/peterbucher/archive/2007/06/22/selfvalidatingcontrol-customcontrol-selber-validieren-lassen.aspx
Hi Peter, thanks for the solution. My thoughts were going in implementing an interface.
I tried it quickly and it seems to work great, mits some changes that I have to look into now ;)
And so, I'm learning every time new stuff :D
BW: I'm from Belgium (flemish) so my german is good enough to understand everything as well ;)