locked
Using RequiredFieldValidator On Custom User Control RRS feed

  • Question

  • User1342168525 posted
    I am sure this one is pretty simple... I have created a simple user control, containing a dropdownlist, called ddlist.ascx. I have it registered and loaded in my aspx page as ddSomething. However, when I am creating my validation controls, ddSomething is not an option when selecting which control to validate. I tried using it anyways, and here is the error I get: Control 'ddSomething' referenced by the ControlToValidate property of 'vldSomething' cannot be validated. How can I get the RequiredFieldValidator to validate my user control along with the rest of the server controls in my webform? Thanks. Bob Gibilaro
    Thursday, March 24, 2005 11:48 AM

All replies

  • User2032526919 posted
    You need to apply ValidationProperty attribute to your user control, its value pointing to the name of the property which will be validated by the validation control. This way validation control at server-side knows which property to look for when validating.
    Thursday, March 24, 2005 1:15 PM
  • User1806792307 posted
    A UserControl itself cannot be validated because it doesn't generate any HTML. You need a control inside it (like that DropDownList) to validate. Effectively, you need to assign the Validator.ControlToValidate property to the ID of the DropDownList (or whatever data entry control you are evaluating.) Unforunately, Microsoft built the Validators so that the ControlToValidate property allows the other control only in the same naming container. A User Control is a naming container. So a validator outside the UserControl cannot assign the ID of any control within it. Initially it sounds like you are stuck. However, there are several ways to handle this: 1. Put the validator inside the UserControl. It can access the ID of the data entry control. 2. Use a CustomValidator. Do not use its ControlToValidate property. Instead, within your own evaluation function you will access the DropDownList through the UserControl. You probably will make the DropDownList field Public so it can be seen once you typecast the UserControl object to the class of that UserControl. 3. Like Teemu said, its possible to use the ValidationPropertyAttribute. You need to add a property to your usercontrol that returns a string value of the data. Suppose that property is called "DDLValue", add this line above the UserControl's class definition (shown in VB.NET): <ValidationProperty("DDLValue")> Public Class MyuserControl Inherits UserControl Since your User Control doesn't generate this property on the client-side, you must turn off client side validation on that validator. Set its EnableClientScript property to false. FYI: I make a replacement to Microsoft's validators that solves most of the issues users face. Professional Validation And More lets you assign the control object as an alternative to the ID. When you do, you can have the validator in a different naming container. That means you can simply put the validator where you want it, attach it to the DropDownList object in the UserControl and it will work.
    Thursday, March 24, 2005 7:23 PM
  • User1342168525 posted
    Thanks everyone! I ended up adding the <ValidationProperty("VALUE")> to my user control as Teemu suggested. To help with that, I checked out this article: http://support.microsoft.com/kb/310082/EN-US/ I then turned off client-side validation on this, as well as my other standard validation controls. Voila! It worked. Thanks again for the help. Bob Gibilaro
    Friday, March 25, 2005 8:14 AM
  • User1187105292 posted

    Ok, I have a question about how to specify the validation property name in this situation. Here's the control:

    <MyControl>
        <MyPropertyIsATextBox />
        <...>
    </MyControl>

    I want the Property "MyPropertyIsATextBox" to be the validation property?

    So, would I use this?:

    [ValidationPropertyAttribute("MyPropertyIsATextBox-Text")]

    Or what? Or is it even possible to make this work?

    Friday, August 17, 2007 12:46 PM
  • User2032526919 posted

    Haven't tested such case, but certainly you could create a top-level property on your MyControl which wraps to the Text property of MyPropertyIsATextBox and then set ValidationProperty attribute to point to this top-level property.

    Friday, August 17, 2007 1:22 PM
  • User156138616 posted

    I had already figured out #1 but #2 was a great way to get around the problem. I think #3 is overkill for my purposes. Thanks very much! Here's a nice example of what I did for #2 that worked perfectly (this is in C#). I set DOSmustBeValid as appropriate for my purposes. Notice that there isn't a ControlToValidate property.

     <asp:CustomValidator ID="DOSmustBeValid" runat="server" OnServerValidate="DOSmustBeValid_ServerValidate" ErrorMessage="The date of service is not recognized as a valid appointment date.">*</asp:CustomValidator>
    <asp:Label ID="DOSwarning" runat="server" ForeColor="White">

        protected void DOSmustBeValid_ServerValidate(object source, ServerValidateEventArgs args)<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>    
        {

            args.IsValid = true;

    <o:p></o:p>        if (DOSwarning.Text != "")

     

    <o:p></o:p>            args.IsValid = false;

     

    <o:p></o:p>    }<o:p></o:p>

     

    Friday, June 6, 2008 3:27 PM
  • User-2094423514 posted

    Peter,

    A question...

    I have a RequiredFieldValidator outside of my user control. The RequiredFieldValidator uses this

    ControlToValidate="MyUserControl:TextBoxDropDown"

     ( where TextBoxDropDown is the name of the server control I want to validate ) . 

    And everything seems to work just fine. So are you certain about...

    A User Control is a naming container. So a validator outside the UserControl cannot assign the ID of any control within it.

    And....

    you must turn off client side validation on that validator

    And do you know about exactly what the colon is doing here ? From the documentation it looks like -as you say- this should not be possible so I imagine its a nasty hack. I would be interested to find out more.

    Thanks in advance

    Wednesday, September 3, 2008 8:14 AM
  • User-2094423514 posted

    Anyone else ?

    Tuesday, September 23, 2008 4:26 AM
  • User1806792307 posted

    I learned one special case since writing my earlier post.

    If you call FindControl on the Page object, you can pass the UniqueID value of the desired control and it will search the control tree.

    In your case, you have the validator on the Page (using the Page as the NamingContainer). The Validator uses the NamingContainer.Findcontrol, so it is using Page.FindControl.

    Second is that formatting. You are mimicking the format of the UniqueID, which is a list of IDs of all naming containers above the control, plus the control itself, separated by a special character. While its typical to have that special character as a $, you have it working with a :. I believe there is a setting to determine that special character and your site probably is using it.

    Tuesday, September 23, 2008 7:06 PM
  • User746069356 posted

    I've been having some trouble with an int field and the Validator. The solution is to make the property int? (i.e. nullable) cause the RequiredFieldValidator checks for null. I post it now cause I have about an age trying to find some way to tell the RequiredFieldValidator to do it against a value (say: 0) with some sort of annotation or something but no way... José.

    Hope it helps some NUGGET like me Tongue out

    Friday, March 5, 2010 11:18 AM
  • User-1199946673 posted

    I've been having some trouble with an int field and the Validator. The solution is to make the property int? (i.e. nullable) cause the RequiredFieldValidator checks for null. I post it now cause I have about an age trying to find some way to tell the RequiredFieldValidator to do it against a value (say: 0) with some sort of annotation or something but no way... José.

    Hope it helps some NUGGET like me Tongue out

     

    If you've questions, please don't open (related) old posts, but start a new thread!

    http://www.mikesdotnetting.com/Article/69/How-to-get-your-forum-question-answered-avoid-thread-jacking

    To answer your question. Together with a required filed validator, also add a compare validator if you want the value to be a specific type (in this case integer) and with a minimum value (0). If you want also a maximum value, then use a rangevalidator.

    Any more questions, please open a new thread, do not respond to this thread anymore?

    Friday, March 5, 2010 11:23 AM
  • User1422214927 posted

    hi anyone that help how can set validator on user id that not accept space betwwen tow mean or exam ubaid expor  so i need when come space then show error validator plz hlep as soon as possible i will thank full of u.

    Tuesday, October 19, 2010 3:44 AM
  • User319153350 posted

    hi, meme, I use colon. but the error is Unable to find control id 'MonthSelector1:TextBox1' referenced by the 'ControlToValidate' property of 'RequiredFieldValidator1'.

    Tuesday, September 4, 2012 11:42 PM
  • User-1199946673 posted

    hi, meme, I use colon. but the error is Unable to find control id 'MonthSelector1:TextBox1' referenced by the 'ControlToValidate' property of 'RequiredFieldValidator1'.

    Didn't you read the entire thread?

    If you've questions, please don't open (related) old posts, but start a new thread!

    http://www.mikesdotnetting.com/Article/69/How-to-get-your-forum-question-answered-avoid-thread-jacking

    Any more questions, please open a new thread, do not respond to this thread anymore?

    Wednesday, September 5, 2012 3:53 AM