Couldn't you put the validator inside the user control as TextBox and
Calendar? Sounds reasonable to me as TextBox really is inside the user
control as well and then you wouldn't need to multiply the validation
because of multiple user controls. Inside the user control you specify the
control id for the validator as you would on normal page, that is the ID of
the control to be validated.
However, if you want to separate the validation, you would perhaps expose
TextBox's Text property as new top-level property in the user control and
apply ValidationProperty attribute for it. That way validation framework
would recognize the ID (of the user control) you provide for the validation
control.
Example of the second approach:
**ASCX of the user control:
<%@ Control Language="c#" AutoEventWireup="false"
Codebehind="DemoUserControl.ascx.cs"
Inherits="ControlDemoApp.DemoUserControl"
TargetSchema="
http://schemas.microsoft.com/intellisense/ie5"%>
<asp:Calendar ID="Calendar1" Runat="server" /><br>
<asp:TextBox ID="TextBox1" Runat="server" />
**Code-behind of the user control (the relevant part
[ValidationProperty("CalendarText")]
public class DemoUserControl : System.Web.UI.UserControl
{
protected System.Web.UI.WebControls.Calendar Calendar1;
protected System.Web.UI.WebControls.TextBox TextBox1;
public string CalendarText
{
get
{
return TextBox1.Text;
}
set
{
TextBox1.Text=value;
}
}
....
}
Then usage on the ASPX Page:
<%@ Page language="c#" Codebehind="login.aspx.cs" AutoEventWireup="false"
Inherits="ControlDemoApp.login" %>
<%@ Register TagPrefix="uc1" TagName="DemoUserControl"
Src="DemoUserControl.ascx" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
</HEAD>
<body >
<form id="Form1" method="post" runat="server">
<uc1
emoUserControl id="DemoUserControl1"
runat="server"></uc1
emoUserControl>
<asp:RequiredFieldValidator id="RequiredFieldValidator1" runat="server"
ErrorMessage="Calendar Date is required"
ControlToValidate="DemoUserControl1"></asp:RequiredFieldValidator>
<asp:CompareValidator id="CompareValidator1" runat="server"
ErrorMessage="Insert a valid date!" ControlToValidate="DemoUserControl1"
Operator="DataTypeCheck" Type="Date"></asp:CompareValidator>
<asp:Button id="Button1" runat="server" Text="Test it"></asp:Button>
</form>
</body>
</HTML>
--
Teemu Keiski
MCP, Designer/Developer
Mansoft tietotekniikka Oy
http://www.mansoft.fi
AspInsiders Member,
www.aspinsiders.com
ASP.NET Forums Moderator,
www.asp.net
AspAlliance Columnist,
www.aspalliance.com