I have a Page in ASP.net and UserControl that I register in this page.
The user control contain a texbox, ajax calendarextender and rangeValidator who valid the date in the textbox.
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="DatePicker.ascx.cs"
Inherits="Root_Webspace.WebControls.DatePicker" %>
<table>
<tr>
<td >
<asp:RangeValidator ID="rvDate" runat="server" Text="!" ErrorMessage="Message..." ControlToValidate="txtDate"
Type="Date" ></asp:RangeValidator>
<ajx:ValidatorCalloutExtender ID="vcalSaleDateRange" runat="server" TargetControlID="rvDate">
</ajx:ValidatorCalloutExtender>
</td>
<td >
<asp:TextBox ID="txtDate" runat="server"></asp:TextBox>
</td>
<td>
<asp:ImageButton ID="imgbtnDate" runat="server" CausesValidation="false" ImageUrl="~/App_Themes/Default/Images/icon-calendar.gif" />
<ajx:CalendarExtender ID="cextDate" runat="server" PopupButtonID="imgbtnDate"
TargetControlID="txtDate">
</ajx:CalendarExtender>
</td>
</tr>
</table>
Code behind:
public TextBox TxtDate
{
get { return txtDate; }
}
The Page contains this user control and CustomValidator. With this CustomValidator , i want to set his ControlToValidate to UserControl textDate.
<%@ Page Title="" Language="C#" MasterPageFile="~/Masterpages/Default.Master" AutoEventWireup="true"
CodeBehind="Test.aspx.cs" Inherits="Root_Webspace.Test" UICulture="auto" Culture="auto"
Async="true" %>
<%@ Register Src="~/WebControls/DatePicker.ascx" TagName="DatePicker" TagPrefix="uc1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnSend" EventName="Click" />
<asp:AsyncPostBackTrigger ControlID="btnCancel" EventName="Click" />
</Triggers>
<ContentTemplate>
<div>
<asp:CustomValidator ID="cvalDate" runat="server" ControlToValidate="How to set with usercontrol txtDate ???? "
Display="Dynamic" Text="!" OnServerValidate="cvalDate_ServerValidate" ValidateEmptyText="true"> </asp:CustomValidator>
<ajx:ValidatorCalloutExtender ID="vcalDateBris" runat="server" TargetControlID="cvalDate">
</ajx:ValidatorCalloutExtender>
<uc1:DatePicker ID="FromDatePicker" runat="server" />
</div>
</ContentTemplate>
</asp:UpdatePanel>
<div>
<div class="buttonzone">
<div id="idButtons" style="margin-right: 5px;">
<asp:Button ID="btnSend" runat="server" TabIndex="10" Text="Send" Width="85px" meta:resourcekey="btnSend"
OnClick="btnSend_Click" />
<asp:Button ID="btnCancel" runat="server" CausesValidation="false" TabIndex="11"
Text="Cancel" OnClick="btnCancel_Click" Width="85px" meta:resourcekey="btnCancel" />
</div>
</div>
</div>
</asp:Content>
Code behind
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) && DateTime.Parse(FromDatePicker.TxtDate.Text) > 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;
}
}
Error => InnerException type : System.Web.HttpException Unable to find control id 'txtDate' referenced by the 'ControlToValidate' property of 'cvalDate'.
When i change to that : cvalDate.ControlToValidate = FromDatePicker.TxtDate.ClientID;
Error=> InnerException type : System.Web.HttpException Unable to find control id 'ctl00_ContentPlaceHolder1_FromDatePicker_txtDate' referenced by the 'ControlToValidate' property of 'cvalDate'.
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.
Any Idea?
Regards.