我创建了一个具有 2 个日期控件(开始日期和结束日期)的用户控件。现在在我的 aspx 页面中,我使用相同的用户控件两次,其中 id 作为父项和子项。现在我希望子用户控件中的日期应该在父用户控件中提供的日期之内。
请参考下面的代码片段,我希望所选日期在日期范围内。
ASCX 页面:
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="EffectiveDate.ascx.cs"
Inherits="UserControlDemo.EffectiveDate" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajaxtoolkit" %>
<asp:ScriptManager runat="server" ID="scriptmanger1" EnablePageMethods="true">
</asp:ScriptManager>
<table id="Table5" width="99%" cellspacing="5">
<tr>
<td valign="middle" width="20%">
<asp:Label ID="Label4" runat="server" Style="white-space: nowrap;" Text="Effective Start Date"></asp:Label>
</td>
<td valign="middle">
<asp:TextBox ID="attPrdStartdate" runat="server"></asp:TextBox>
<asp:Label ID="Label8" runat="server" ForeColor="Red" Text="*"></asp:Label>
<asp:ImageButton ID="Image3" runat="server" ImageUrl="/Images/CalendarImage.png"
Style="margin-bottom: -5px" />
<ajaxtoolkit:FilteredTextBoxExtender ID="FilteredTextBoxExtender5" runat="server"
TargetControlID="attPrdStartdate" ValidChars="1234567890/" />
<ajaxtoolkit:CalendarExtender ID="CalendarExtender3" runat="server" TargetControlID="attPrdStartdate"
PopupButtonID="Image3" Format="MM/dd/yyyy">
</ajaxtoolkit:CalendarExtender>
<asp:CompareValidator ID="CompareValidator5" runat="server" ControlToValidate="attPrdStartdate"
Display="Dynamic" ErrorMessage="Invalid Date" ForeColor="Red" Operator="DataTypeCheck"
Type="Date" Style="font-size: smaller">
</asp:CompareValidator>
<asp:RangeValidator ID="RangeValidator1" runat="server" ControlToValidate="attPrdStartdate" Type="Date"
Display="Dynamic" ErrorMessage="Dates out of range" ForeColor="Red" EnableClientScript="False"></asp:RangeValidator>
</td>
</tr>
<tr>
<td valign="middle">
<asp:Label ID="Label5" runat="server" Style="white-space: nowrap;" Text="Effective End Date"></asp:Label>
</td>
<td valign="middle">
<asp:TextBox ID="attPrdEnddate" runat="server"></asp:TextBox>
<asp:Label ID="Label9" runat="server" ForeColor="Red" Text="*"></asp:Label>
<ajaxtoolkit:FilteredTextBoxExtender ID="FilteredTextBoxExtender6" runat="server"
TargetControlID="attPrdEnddate" ValidChars="1234567890/" />
<ajaxtoolkit:CalendarExtender ID="CalendarExtender4" runat="server" TargetControlID="attPrdEnddate"
PopupButtonID="Image4" Format="MM/dd/yyyy">
</ajaxtoolkit:CalendarExtender>
<asp:ImageButton ID="Image4" runat="server" ImageUrl="/Images/CalendarImage.png"
Style="margin-bottom: -5px" />
<asp:CompareValidator ID="CompareValidator6" runat="server" ControlToValidate="attPrdEnddate"
Display="Dynamic" ErrorMessage="Invalid Date" ForeColor="Red" ControlToCompare="attPrdStartdate"
Operator="GreaterThan" Type="Date" Style="font-size: smaller">
</asp:CompareValidator>
<asp:RangeValidator ID="RangeValidator2" runat="server" ControlToValidate="attPrdEnddate" Type="Date"
Display="Dynamic" ErrorMessage="Dates out of range" ForeColor="Red" EnableClientScript="False"></asp:RangeValidator>
</td>
</tr>
<tr>
<td colspan="2">
<asp:Label runat="server" ID="errlblBaseln" Text="" ForeColor="Red" Font-Size="smaller"></asp:Label>
</td>
</tr>
</table>
背后的 ASCX 代码:
namespace UserControlDemo
{
public partial class EffectiveDate : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
RangeValidator1.MaximumValue = endDate.ToShortDateString();
RangeValidator1.MinimumValue = startDate.ToShortDateString();
RangeValidator2.MaximumValue = endDate.ToShortDateString();
RangeValidator2.MinimumValue = startDate.ToShortDateString();
}
private DateTime startDate;
private DateTime endDate;
public DateTime StartDate
{
get { return startDate; }
set { startDate = value; }
}
public DateTime EndDate
{
get { return endDate; }
set { endDate = value; }
}
}
}
我的 aspx 页面:
<uc:EfectiveDate ID="MyDates" runat="server" StartDate="01/01/2013" EndDate="12/12/2013" />