Last post Mar 30, 2009 03:19 AM by Allen Chen – MSFT
Mar 25, 2009 03:21 PM|wyx2000|LINK
I want to make my usercontrol support validation. I did something like this, for example, I have a customized textbox usercontrol, I put a textbox in the usercontrol, and I add a RequiredFieldValidator in it and link it with the Textbox, assign ValidationGroup
and message etc., and then it works.
But now I have a usercontrol, in the usercontrol, I need check content of a uploaded file and if the content is not what I want, I want the usercontrol to show a error message, and the FormView where the usercontrol sit should not submit.
I tried <%@ Implements Interface="System.Web.UI.IValidator"%> in the usercontrol , and add code like
public bool IsValid
But it doesn't work, the FormView always submit even IsValid in the usercontrol always return false.
Also, it seems IValidator doesn't contain ValidationGroup. I tried to add that property to the usercontrol, and I tried to remove ValidationGroup from the submit button. But I could never make the FormView stay unsubmitted.
Mar 25, 2009 05:55 PM|karthi128|LINK
public bool IsValid
Mar 25, 2009 08:15 PM|wyx2000|LINK
Not sure what you mean, I let it always return false to test, so that it can keep the FormView from submitting and show error message. But I didn't see that expected result.
Mar 26, 2009 10:37 PM|Allen Chen – MSFT|LINK
If my understanding is correct what you want to do is to avoid postback when the content of the file is invalid (for instance, the size of the file is too large). If this is true it's impossible unless you're using RIA techniques. When you get the file
at server side you've already posted the form. The validation you do is actually server side validation.
There're some workarounds:
1. Use Silverlight. You can check the file stream at client side in this case, and do client side valication as well.
2. AJAX. This can help to avoid page refresh. The data is still posted to server. You can do server side validation as normal.
Mar 29, 2009 03:49 PM|wyx2000|LINK
Thanks for you reply.
I think you didn't get me right. I don't want to prevent postback. I just want to prevent the form is submitted. I think I just need a server side validation. Just like the required validation. You can validate it on client side, but for safety and browser
compatibility, we usually add a server side validation too. When it passes client side validation unexpectedly for some reason, we can check it on server side, and if it doesn't go through, we still show error message and the FormView where the field is on
will not submit to backend even the form is posted back.
I can simply add a required validation to my usercontrol to prevent the FormView the usercontrol is in from submitting. But required validation need bind to a control like textbox which I don;t have, so I am trying to implment the validator interface in
usercontrol. That is what I could not figure out
Mar 29, 2009 09:46 PM|Allen Chen – MSFT|LINK
If my understanding is correct, what you want to do is to use RequiredFieldValidator to validate a custom property in your WebUserControl. If so we can try following way to do so:
Mar 29, 2009 09:54 PM|wyx2000|LINK
That is a way to do it, but I want to make the usercontrol self contained. That means I can just put the usercontrol in a FormView without worrying about any validator.
As I said, I can out a required validator into the usercontrol, and write some code to bind it with asp.net control in my usercontrol. But in this usercontrol. I don't have asp.net control like TextBox, I only has a File control in it. I can not bind required
validator to the File control. I need check the file control to decide if the usercontrol is valid. And that is why I tried to implement the Validator interface in the usercontrol.
Mar 29, 2009 10:17 PM|Allen Chen – MSFT|LINK
I think you can put a
Mar 30, 2009 02:11 AM|wyx2000|LINK
You still need set "ControlToValidate" of the CustomValidator control, I don't think it is problem to set the File control to ControlToValidate.
Don't you think I should implement IValidator in this case?
Mar 30, 2009 03:19 AM|Allen Chen – MSFT|LINK