我在 ASP.net 和 UserControl 中有一个页面,我在此页面中注册。
用户控件包含一个 texbox、ajax calendarextender 和 rangeValidator,它们验证文本框中的日期。
<%@ 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>
后面的代码:
public TextBox TxtDate
{
get { return txtDate; }
}
该页面包含此用户控件和 CustomValidator。有了这个 CustomValidator ,我想将他的 ControlToValidate 设置为 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>
背后的代码
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;
}
}
错误 => InnerException 类型:System.Web.HttpException 找不到由“cvalDate”的“ControlToValidate”属性引用的控件 ID“txtDate”。
当我更改为: cvalDate.ControlToValidate = FromDatePicker.TxtDate.ClientID;
错误=> InnerException 类型:System.Web.HttpException 找不到由“cvalDate”的“ControlToValidate”属性引用的控件 ID“ctl00_ContentPlaceHolder1_FromDatePicker_txtDate”。
如果我不设置 cvalDate.controlToValidate,则 vcalDateBris (ValidatorCalloutExtender) 在必须显示无效日期消息时将失败,因为此 ValidatorCalloutExtender 通过 TargetControlID 链接到 CustomValidator,其中 CustomValidator 没有 controlToValidate 控件集。那么如何将Page中CustomValidator的ControlToValidate设置为FromDatePicker(USerControl)内部的txtDate,以在ValidatorCalloutExtender中显示消息。
任何想法?
问候。