0

我创建了一个具有 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" />
4

1 回答 1

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
    }

希望这会帮助你。

于 2013-08-29T13:45:06.420 回答