我在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;
}
}
错误=&gt; InnerException类型:System.Web.HttpException无法找到'cvalDate'的'ControlToValidate'属性引用的控件ID'txtDate'。
当我改为:cvalDate.ControlToValidate = FromDatePicker.TxtDate.ClientID;
错误=&GT; InnerException类型:System.Web.HttpException无法找到'cvalDate'的'ControlToValidate'属性引用的控件ID'ctl00_ContentPlaceHolder1_FromDatePicker_txtDate'。
如果我没有设置cvalDate.controlToValidate,则当必须显示无效日期msg时,vcalDateBris(ValidatorCalloutExtender)将失败,因为此ValidatorCalloutExtender通过TargetControlID链接到CustomValidator,其中CustomValidator没有controlToValidate控件集。 那么如何将Page中的CustomValidator的ControlToValidate设置为FromDatePicker(USerControl)内部的txtDate,以便在ValidatorCalloutExtender中显示消息。
任何想法?
的问候。
答案 0 :(得分:0)
ControlToValidate="FromDatePicker$txtDate"