Asp.NET用户控件日期验证

时间:2013-08-29 12:51:48

标签: asp.net user-controls

我创建了一个用户控件,它有2个日期控件(开始日期和结束日期)。现在在我的aspx页面中,我使用相同的用户控件两次,id为parent和child。现在我希望子用户控件中的日期应该在父用户控件中提供的日期之内。

请参阅以下代码段,我希望所选日期在日期范围内。

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 Code Behind:

    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" />

1 个答案:

答案 0 :(得分:0)

我认为你正试图做这样的事情。

<强>父

    <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="Parent.ascx.cs" Inherits="StackOverFlow.Parent" %>
<table>
    <tr>
        <td>Parent Start Date </td>
        <td>
            <asp:TextBox runat="server" ID="txtStartDate" />
        </td>
    </tr>
    <tr>
        <td>Parent End Date </td>
        <td>
            <asp:TextBox runat="server" ID="txtEndDate" />
        </td>
    </tr>
</table>

儿童

    <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="Child.ascx.cs" Inherits="StackOverFlow.Child" %>
<table>
    <tr>
        <td>Child Start Date </td>
        <td>
            <asp:TextBox runat="server" ID="txtStartDate" />
        </td>
    </tr>
    <tr>
        <td>Child End Date </td>
        <td>
            <asp:TextBox runat="server" ID="txtEndDate" />
        </td>
    </tr>
</table>

您的信息页

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="StackOverFlow.WebForm1" %>

<%@ Register TagPrefix="p" Src="~/Parent.ascx" TagName="Parent" %>
<%@ Register TagPrefix="c" Src="~/Child.ascx" TagName="Child" %>
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <table>
                <tr>
                    <td>
                        <p:Parent ID="parent" runat="server"></p:Parent>
                    </td>

                </tr>
                <tr>
                    <td>
                        <c:Child ID="child" runat="server"></c:Child>
                    </td>
                </tr>
                <tr><td>
                    <asp:Button Text="Compare" ID="btnCompare" runat="server" OnClick="btnCompare_Click" /></td></tr>
            </table>
        </div>
    </form>
</body>
</html>

代码

protected void btnCompare_Click(object sender, EventArgs e)
    {
        TextBox txtParentStartDate = (TextBox)parent.FindControl("txtStartDate");

        if (txtParentStartDate!=null)
        {
            DateTime dtParentStartDate = Convert.ToDateTime(txtParentStartDate.Text.Trim());
        }

        //likewise get other dates and compare
    }

希望这会对你有所帮助。