controltovalidate property of customvalidator is incorrect

Discussion in 'ASP .Net Web Controls' started by TS, Jul 11, 2007.

  1. TS

    TS Guest

    hello, i have an inherited Textbox control that adds a customvalidator to
    its controls. This works fine. I have another composite control (radio
    button list) that contains this textbox control. I have a public property on
    this control that is used to set the CustomValidator.ControlToValidate with
    the correct ID. This validator when run is trying to validate the radio
    button list control instead of the correct textbox.

    I notice that the .ControlToValidate property is set correctly still at
    addAttributesToRender event, but the rendered html for the validators sets
    this validator's controltovalidate to the clientId of the radiobuttonlist
    control. I wonder why it happens. like I said, this textbox control and its
    validator works correctly by itself, its only when i include the textbox
    inside my radiobuttonlist control that the validator uses the incorrect
    controltovalidate.

    is there a way i can see why this is getting done - i can't debug it because
    its microsoft code.

    thansk
     
    TS, Jul 11, 2007
    #1
    1. Advertisements

  2. Hi TS,

    From your description, you've built a custom compostive control(which
    contains a textbox), currently, when you use a custom validtor to validate
    the Textbox (inside that composite control), you get error indicate the
    textbox can not be located, correct?

    Based on my understanding, the problem should be caused by the how the
    validator control will try locating the "ControlToValidate" control
    instance. the Validator controls will only use "ControlToValidate" as ID
    and call FindControl at the current Parent namingContainer, below is the
    code logic of the BaseValidator(from reflector):

    =========================
    protected void CheckControlValidationProperty(string name, string
    propertyName)
    {
    Control component = this.NamingContainer.FindControl(name);
    if (component == null)
    {
    throw new HttpException(SR.GetString("Validator_control_not_found",
    new object[] { name, propertyName, this.ID }));
    }
    if (GetValidationProperty(component) == null)
    {
    throw new HttpException(SR.GetString("Validator_bad_control_type",
    new object[] { name, propertyName, this.ID }));
    }
    }
    =========================

    Therefore, I'm afraid you can not let validtor control directly locate the
    inner textbox nested in your Composite control. For your scenario, I think
    you may implement the validation(for the inner TextBox as below):

    ** Add an dedicated public property on the composite control which expose
    the Textbox's "Text" property value

    ** Apply "ValidationPropertyAttribute" on the composite control and specify
    the dedicatd property (defined above) as the validation property.

    #ValidationPropertyAttribute Class
    http://msdn2.microsoft.com/en-us/library/system.web.ui.validationpropertyatt
    ribute(VS.71).aspx

    Thus, you can make other validator controls validate against the dedicated
    property of the composite so as to validate the value of the inner TextBox.
    How do you think?

    Sincerely,

    Steven Cheng

    Microsoft MSDN Online Support Lead



    ==================================================

    Get notification to my posts through email? Please refer to
    http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
    ications.



    Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
    where an initial response from the community or a Microsoft Support
    Engineer within 1 business day is acceptable. Please note that each follow
    up response may take approximately 2 business days as the support
    professional working with you may need further investigation to reach the
    most efficient resolution. The offering is not appropriate for situations
    that require urgent, real-time or phone-based interactions or complex
    project analysis and dump analysis issues. Issues of this nature are best
    handled working with a dedicated Microsoft Support Engineer by contacting
    Microsoft Customer Support Services (CSS) at
    http://msdn.microsoft.com/subscriptions/support/default.aspx.

    ==================================================


    This posting is provided "AS IS" with no warranties, and confers no rights.
     
    Steven Cheng[MSFT], Jul 12, 2007
    #2
    1. Advertisements

  3. TS

    TS Guest

    thanks, i tried that, but it didn't work. The validation works, but when the
    client side validation runs, it validates the selected value of clientsigned
    (radiobuttonlist) instead of the text of the textbox.

    note that my composite control is an inherited radioButtonList. Note that
    DateBox is a Textbox that adds the validation controls i'm interested in, so
    my radiobuttonlist contains a subclassed Textbox which contains some
    validators. In the textbox, i set it's validator's controlToValidate
    property with the textbox's ID; i'm thinking that somewhere the validator
    chooses to use the parent (clientSigned) as its control to validate as
    evidence by the rendered html at the bottom.

    thanks!


    [ValidationPropertyAttribute("DateText")]
    public class ClientSigned : DSHS.SNF.Web.Control.RadioButtonList,
    DSHS.SNF.Web.Control.IInputControl
    {

    DSHS.SNF.Web.Control.DateBox dbBox;

    DSHS.SNF.Web.Control.Label lblDate;


    public ClientSigned()

    : base()

    {

    this.Items.Add(new ListItem("Yes", "Y"));

    // add "no" and make it default selected

    ListItem no = new ListItem("No", "N");

    no.Selected = true;

    this.Items.Add(no);

    }

    public string DateText

    {

    get { return dbBox.Text; }

    set { dbBox.Text = value; }

    }

    public string dateControlID = string.Empty;

    public string DateControlID

    {

    get { return dateControlID; }

    set { dateControlID = value; }

    }

    protected override void CreateChildControls()

    {

    base.CreateChildControls();



    lblDate = new Label();

    lblDate.ID = string.Format("lbl{0}", DateControlID);

    lblDate.Text = "Date: ";

    dbBox = new DateBox();

    dbBox.ID = DateControlID;

    dbBox.Columns = 12;

    dbBox.ValidatorLabel = ValidatorLabel;

    dbBox.IsRequired = IsRequired;

    dbBox.ValidationGroup = ValidationGroup;

    dbBox.ValidationGroup1 = ValidationGroup1;

    Controls.Add(dbBox);

    }}

    This is the the stuff asp.net rendered for the validator. Notice the
    controltovalidate points to the client id of clientsigned instead of the
    actual textbox whose client id is:
    ctl00_ctl00_ContentMainPlaceHolder_ApplicationContentMainPlaceHolder_ClientS
    igned_ClientSignatureDate

    var
    ctl00_ctl00_ContentMainPlaceHolder_ApplicationContentMainPlaceHolder_ClientS
    igned_DVClientSignatureDate = document.all ?
    document.all["ctl00_ctl00_ContentMainPlaceHolder_ApplicationContentMainPlace
    Holder_ClientSigned_DVClientSignatureDate"] :
    document.getElementById("ctl00_ctl00_ContentMainPlaceHolder_ApplicationConte
    ntMainPlaceHolder_ClientSigned_DVClientSignatureDate");
    ctl00_ctl00_ContentMainPlaceHolder_ApplicationContentMainPlaceHolder_ClientS
    igned_DVClientSignatureDate.controltovalidate =
    "ctl00_ctl00_ContentMainPlaceHolder_ApplicationContentMainPlaceHolder_Client
    Signed";
    ctl00_ctl00_ContentMainPlaceHolder_ApplicationContentMainPlaceHolder_ClientS
    igned_DVClientSignatureDate.focusOnError = "t";
    ctl00_ctl00_ContentMainPlaceHolder_ApplicationContentMainPlaceHolder_ClientS
    igned_DVClientSignatureDate.display = "Dynamic";
    ctl00_ctl00_ContentMainPlaceHolder_ApplicationContentMainPlaceHolder_ClientS
    igned_DVClientSignatureDate.evaluationfunction =
    "CustomValidatorEvaluateIsValid";
    ctl00_ctl00_ContentMainPlaceHolder_ApplicationContentMainPlaceHolder_ClientS
    igned_DVClientSignatureDate.clientvalidationfunction = "ValidateDate";
    ctl00_ctl00_ContentMainPlaceHolder_ApplicationContentMainPlaceHolder_ClientS
    igned_DVClientSignatureDate.validateemptytext = "true";



    "Steven Cheng[MSFT]" <> wrote in message
    news:...
    > Hi TS,
    >
    > From your description, you've built a custom compostive control(which
    > contains a textbox), currently, when you use a custom validtor to validate
    > the Textbox (inside that composite control), you get error indicate the
    > textbox can not be located, correct?
    >
    > Based on my understanding, the problem should be caused by the how the
    > validator control will try locating the "ControlToValidate" control
    > instance. the Validator controls will only use "ControlToValidate" as ID
    > and call FindControl at the current Parent namingContainer, below is the
    > code logic of the BaseValidator(from reflector):
    >
    > =========================
    > protected void CheckControlValidationProperty(string name, string
    > propertyName)
    > {
    > Control component = this.NamingContainer.FindControl(name);
    > if (component == null)
    > {
    > throw new

    HttpException(SR.GetString("Validator_control_not_found",
    > new object[] { name, propertyName, this.ID }));
    > }
    > if (GetValidationProperty(component) == null)
    > {
    > throw new HttpException(SR.GetString("Validator_bad_control_type",
    > new object[] { name, propertyName, this.ID }));
    > }
    > }
    > =========================
    >
    > Therefore, I'm afraid you can not let validtor control directly locate the
    > inner textbox nested in your Composite control. For your scenario, I think
    > you may implement the validation(for the inner TextBox as below):
    >
    > ** Add an dedicated public property on the composite control which expose
    > the Textbox's "Text" property value
    >
    > ** Apply "ValidationPropertyAttribute" on the composite control and

    specify
    > the dedicatd property (defined above) as the validation property.
    >
    > #ValidationPropertyAttribute Class
    >

    http://msdn2.microsoft.com/en-us/library/system.web.ui.validationpropertyatt
    > ribute(VS.71).aspx
    >
    > Thus, you can make other validator controls validate against the dedicated
    > property of the composite so as to validate the value of the inner

    TextBox.
    > How do you think?
    >
    > Sincerely,
    >
    > Steven Cheng
    >
    > Microsoft MSDN Online Support Lead
    >
    >
    >
    > ==================================================
    >
    > Get notification to my posts through email? Please refer to
    >

    http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
    > ications.
    >
    >
    >
    > Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
    > where an initial response from the community or a Microsoft Support
    > Engineer within 1 business day is acceptable. Please note that each follow
    > up response may take approximately 2 business days as the support
    > professional working with you may need further investigation to reach the
    > most efficient resolution. The offering is not appropriate for situations
    > that require urgent, real-time or phone-based interactions or complex
    > project analysis and dump analysis issues. Issues of this nature are best
    > handled working with a dedicated Microsoft Support Engineer by contacting
    > Microsoft Customer Support Services (CSS) at
    > http://msdn.microsoft.com/subscriptions/support/default.aspx.
    >
    > ==================================================
    >
    >
    > This posting is provided "AS IS" with no warranties, and confers no

    rights.
    >
    >
     
    TS, Jul 12, 2007
    #3
  4. Thanks for your reply TS,

    So the actual control structure of your composite control is as below:

    **Your custom RadioButtonList control is the toplevel container control

    **A DateBox child control is nested in RadioButtonList control and it is
    also a composte control (inherit from TextBox ) and contains some validator
    controls

    Is my understanding consistent to your current page's control structure?

    As I mentioned earilier, validator control must be put at the same level
    with the target control it want to validate. Thus, for your scenario, if
    your DateBox inherit from TextBox, and it also contains child validators
    that will validate itself(since it is the TextBox), I'm afraid this may
    raise problem. I suggest you try the following approach to implement the
    DateBox:

    ** Define DateBox as a custom composite control derived from
    CompositeControl(or WebControl and implement INamingContainer), do not
    inherit from TextBox

    ** Add a TextBox and Validators into the DateBox as child controls.

    ** Assign the TextBox's ID to those validators's ControlToValidate
    property. Since they're at the same control structure level, the validator
    should be able to locate the TextBox correctlty.

    Sincerely,

    Steven Cheng

    Microsoft MSDN Online Support Lead


    This posting is provided "AS IS" with no warranties, and confers no rights.
     
    Steven Cheng[MSFT], Jul 13, 2007
    #4
  5. TS

    TS Guest

    you are correct about my current structure.

    The date box when used by itself (not nested inside the Radiobuttonlist) and
    contains validators works fine, the validators find it correctly even though
    the validators are added as controls of the DateBox. There is no issue in
    this scenario, the only issue is when I add this control to my
    radiobuttonlist control is when the validator can't find the textbox
    anymore.

    i'd prefer to keep it as an inherited control since i don't have to
    re-expose all the properties since they come by default with using a
    radiobuttonlist.

    Currently i have no way to debug why this is happening. Is there event i can
    tie into that will help shed some light on why the rendered
    validator.controltovalidate property points to the clientID of the main
    container (radiobuttonlist)? Maybe i can override a base method so i can
    control how the RENDERED controltovalidate property of the rendered
    validator gets set?

    thanks

    "Steven Cheng[MSFT]" <> wrote in message
    news:...
    > Thanks for your reply TS,
    >
    > So the actual control structure of your composite control is as below:
    >
    > **Your custom RadioButtonList control is the toplevel container control
    >
    > **A DateBox child control is nested in RadioButtonList control and it is
    > also a composte control (inherit from TextBox ) and contains some

    validator
    > controls
    >
    > Is my understanding consistent to your current page's control structure?
    >
    > As I mentioned earilier, validator control must be put at the same level
    > with the target control it want to validate. Thus, for your scenario, if
    > your DateBox inherit from TextBox, and it also contains child validators
    > that will validate itself(since it is the TextBox), I'm afraid this may
    > raise problem. I suggest you try the following approach to implement the
    > DateBox:
    >
    > ** Define DateBox as a custom composite control derived from
    > CompositeControl(or WebControl and implement INamingContainer), do not
    > inherit from TextBox
    >
    > ** Add a TextBox and Validators into the DateBox as child controls.
    >
    > ** Assign the TextBox's ID to those validators's ControlToValidate
    > property. Since they're at the same control structure level, the validator
    > should be able to locate the TextBox correctlty.
    >
    > Sincerely,
    >
    > Steven Cheng
    >
    > Microsoft MSDN Online Support Lead
    >
    >
    > This posting is provided "AS IS" with no warranties, and confers no

    rights.
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
     
    TS, Jul 13, 2007
    #5
  6. Thanks for your reply TS,

    For for current implementation, the custom DateBox directly inherited from
    TextBox, one thing I've noticed is that TextBox control is a simple
    WebControl which doesn't implement INamingContainer interface, that means
    if you add other child controls into its Controls collection(like composite
    control), it may cause problem. I suggest you try making your custom
    DateBox control implement the INamingContainer interface and test it to see
    whether it make any difference.

    Sincerely,

    Steven Cheng

    Microsoft MSDN Online Support Lead


    This posting is provided "AS IS" with no warranties, and confers no rights.
     
    Steven Cheng[MSFT], Jul 17, 2007
    #6
  7. Hi TS,

    Have you got any progress on this issue or does the info in my last reply
    helps some? If there is any further questions, please don't hesitate to
    post here.

    Sincerely,

    Steven Cheng

    Microsoft MSDN Online Support Lead


    This posting is provided "AS IS" with no warranties, and confers no rights.
     
    Steven Cheng[MSFT], Jul 19, 2007
    #7
  8. TS

    TS Guest

    i guess i'm satisfied, thanks

    "Steven Cheng[MSFT]" <> wrote in message
    news:...
    > Hi TS,
    >
    > Have you got any progress on this issue or does the info in my last reply
    > helps some? If there is any further questions, please don't hesitate to
    > post here.
    >
    > Sincerely,
    >
    > Steven Cheng
    >
    > Microsoft MSDN Online Support Lead
    >
    >
    > This posting is provided "AS IS" with no warranties, and confers no

    rights.
    >
     
    TS, Jul 19, 2007
    #8
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steven Nagy

    Custom Control needs ControlToValidate

    Steven Nagy, Mar 6, 2007, in forum: ASP .Net
    Replies:
    4
    Views:
    669
    MasterGaurav \(www.edujini-labs.com\)
    May 8, 2007
  2. DaveyP
    Replies:
    1
    Views:
    771
    Chris Shepherd
    Apr 18, 2008
  3. Nathan Sokalski
    Replies:
    0
    Views:
    444
    Nathan Sokalski
    Mar 8, 2010
  4. Jeff
    Replies:
    6
    Views:
    296
    KMILO
    May 2, 2005
  5. TS

    controlToValidate property of compareValidator

    TS, Dec 30, 2005, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    256
    Steven Cheng[MSFT]
    Jan 3, 2006
Loading...

Share This Page