我在這個頁面中註冊了ASP.net和UserControl中的頁面。設置頁面中的CustomValidator的ControlToValidate爲USercontrol內的文本框,以顯示ValidatorCalloutExtender消息
用戶控件包含在文本框中有效日期的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。
後面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型
<%@ 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>
代碼信息:System.Web.HttpException無法找到控制ID 'txtDate' 由 'cvalDate' 的 '的ControlToValidate' 屬性引用。
當我改變到:cvalDate.ControlToValidate = FromDatePicker.TxtDate.ClientID;
Error => InnerException類型:System.Web.HttpException無法找到由'cvalDate'的'ControlToValidate'屬性引用的控件ID'ctl00_ContentPlaceHolder1_FromDatePicker_txtDate'。
如果我不設置cvalDate.controlToValidate,該vcalDateBris(ValidatorCalloutExtender)將當它必須顯示無效日期味精失敗,因爲這ValidatorCalloutExtender鏈接的CustomValidator通過的TargetControlID其中的CustomValidator沒有的ControlToValidate控制設定。 那麼如何將頁面中的CustomValidator的ControlToValidate設置爲FromDatePicker(USerControl)內部的txtDate,以便在ValidatorCalloutExtender中顯示消息。
任何想法?
問候。
非常感謝你,它的工作原理。在您回答之前,我使用ContentPlaceHolder1 $ FromDatePicker $ txtDate測試了誰是控件的唯一標識。看起來FromDatePicker $ txtDate是這個UniqueID的一部分。你知道它爲什麼注意與UniqueID ??? –