【发布时间】:2013-09-01 22:08:12
【问题描述】:
我创建了一个用户控件,它有 2 个日期控件(开始日期和结束日期)。现在在我的 aspx 页面中,我使用相同的用户控件两次,其中 id 作为父项和子项。现在我希望子用户控件中的日期应该在父用户控件中提供的日期之内。
请参考以下代码sn-p,我希望所选日期在日期范围内。
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" />
【问题讨论】:
-
什么样的日期控件?请告诉我们你有什么。另外,在这种情况下,父母/孩子是什么意思?
-
假设用户选择开始日期为 2013 年 1 月 1 日,结束日期为 2014 年 12 月 12 日。因此,在子用户控件中,用户应该只能在 2013 年 1 月 1 日到 2014 年 12 月 12 日之间选择日期
-
在此处显示您的 *.aspx 标记代码
-
@insomnium_ 我已经在上面的部分添加了aspx代码
标签: asp.net user-controls