Set ControlToValidate of CustomValidator in Page for a textbox inside USercontrol to display message with ValidatorCalloutExtender
<p>I have a Page in ASP.net and UserControl that I register in this page.</p>

<p>The user control contain a texbox, ajax calendarextender and rangeValidator who valid the date in the textbox.</p>

<pre><code>&lt;%@ Control Language="C#" AutoEventWireup="true" CodeBehind="DatePicker.ascx.cs"
Inherits="Root_Webspace.WebControls.DatePicker" %&gt;


&lt;table&gt;
&lt;tr&gt;
    &lt;td  &gt;
        &lt;asp:RangeValidator ID="rvDate" runat="server" Text="!" ErrorMessage="Message..." ControlToValidate="txtDate"
            Type="Date" &gt;&lt;/asp:RangeValidator&gt;
        &lt;ajx:ValidatorCalloutExtender ID="vcalSaleDateRange" runat="server" TargetControlID="rvDate"&gt;
                                    &lt;/ajx:ValidatorCalloutExtender&gt;
    &lt;/td&gt;
    &lt;td &gt;
        &lt;asp:TextBox ID="txtDate"   runat="server"&gt;&lt;/asp:TextBox&gt;
    &lt;/td&gt;
    &lt;td&gt;
        &lt;asp:ImageButton ID="imgbtnDate" runat="server" CausesValidation="false" ImageUrl="~/App_Themes/Default/Images/icon-calendar.gif" /&gt;
        &lt;ajx:CalendarExtender ID="cextDate"  runat="server" PopupButtonID="imgbtnDate"
                                        TargetControlID="txtDate"&gt;
        &lt;/ajx:CalendarExtender&gt;
    &lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
</code></pre>

<p>Code behind:</p>

<pre><code>public TextBox TxtDate
    {
        get { return txtDate; }
    }
</code></pre>

<p>The Page contains this user control and CustomValidator.
With this CustomValidator , i want to set his ControlToValidate to UserControl textDate.</p>

<pre><code>&lt;%@ Page Title="" Language="C#" MasterPageFile="~/Masterpages/Default.Master" AutoEventWireup="true"
CodeBehind="Test.aspx.cs" Inherits="Root_Webspace.Test" UICulture="auto" Culture="auto"
Async="true" %&gt;

&lt;%@ Register Src="~/WebControls/DatePicker.ascx" TagName="DatePicker" TagPrefix="uc1" %&gt;
&lt;asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server"&gt;
&lt;/asp:Content&gt;
&lt;asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server"&gt;

&lt;asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional"&gt;
    &lt;Triggers&gt;
        &lt;asp:AsyncPostBackTrigger ControlID="btnSend" EventName="Click" /&gt;
        &lt;asp:AsyncPostBackTrigger ControlID="btnCancel" EventName="Click" /&gt;
    &lt;/Triggers&gt;
    &lt;ContentTemplate&gt;
        &lt;div&gt;
            &lt;asp:CustomValidator ID="cvalDate" runat="server" ControlToValidate="How to set with usercontrol txtDate ???? "
                Display="Dynamic" Text="!" OnServerValidate="cvalDate_ServerValidate" ValidateEmptyText="true"&gt; &lt;/asp:CustomValidator&gt;
            &lt;ajx:ValidatorCalloutExtender ID="vcalDateBris" runat="server" TargetControlID="cvalDate"&gt;
            &lt;/ajx:ValidatorCalloutExtender&gt;
            &lt;uc1:DatePicker ID="FromDatePicker" runat="server" /&gt;
        &lt;/div&gt;
          &lt;/ContentTemplate&gt;
&lt;/asp:UpdatePanel&gt;
&lt;div&gt;
    &lt;div class="buttonzone"&gt;
        &lt;div id="idButtons" style="margin-right: 5px;"&gt;
            &lt;asp:Button ID="btnSend" runat="server" TabIndex="10" Text="Send" Width="85px" meta:resourcekey="btnSend"
                OnClick="btnSend_Click" /&gt;
            &lt;asp:Button ID="btnCancel" runat="server" CausesValidation="false" TabIndex="11"
                Text="Cancel" OnClick="btnCancel_Click" Width="85px" meta:resourcekey="btnCancel" /&gt;
        &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;
&lt;/asp:Content&gt;
</code></pre>

<p>Code behind</p>

<pre><code>    protected void btnSend_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            //Do Something
        }
    }

    protected void cvalDate_ServerValidate(object source, ServerValidateEventArgs args)
    {
        cvalDate.ErrorMessage = string.Empty;
        args.IsValid = true;
        if (!IsDate(this.FromDatePicker.TxtDate.Text))
            cvalDate.ErrorMessage = Root.Resources.WebMessages.INVALIDDATE;
        else if (!string.IsNullOrEmpty(FromDatePicker.TxtDate.Text) &amp;&amp; DateTime.Parse(FromDatePicker.TxtDate.Text) &gt; DateTime.Now)
            cvalDate.ErrorMessage = Root.Resources.WebMessages.BIGGERTHAN;


        args.IsValid = string.IsNullOrEmpty(cvalDate.ErrorMessage);
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            cvalDate.ControlToValidate = FromDatePicker.TxtDate.ID;
        }
    }
</code></pre>

<p>Error => InnerException type : System.Web.HttpException Unable to find control id 'txtDate' referenced by the 'ControlToValidate' property of 'cvalDate'.</p>

<p>When i change to that : cvalDate.ControlToValidate = FromDatePicker.TxtDate.ClientID;</p>

<p>Error=> InnerException type : System.Web.HttpException Unable to find control id 'ctl00_ContentPlaceHolder1_FromDatePicker_txtDate' referenced by the 'ControlToValidate' property of 'cvalDate'.</p>

<p>If I don’t set the cvalDate.controlToValidate, the vcalDateBris (ValidatorCalloutExtender) will fail when it have to display the invalid date msg, because this ValidatorCalloutExtender link to CustomValidator by TargetControlID where CustomValidator does not have controlToValidate control set.
So how can i set ControlToValidate of CustomValidator in Page to txtDate who is inside FromDatePicker (USerControl), for display message inside ValidatorCalloutExtender.</p>

<p>Any Idea?</p>

<p>Regards. </p>
