【问题标题】:Asp.NET user control date validationAsp.NET 用户控件日期验证
【发布时间】: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


【解决方案1】:

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

家长

    <%@ 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
    }

希望这会对你有所帮助。

【讨论】:

  • 非常感谢您的努力。万分感激。我修改了问题,请看一下并提出建议。我正在尝试使用范围验证器来实现这一点。
  • @AmitAgarwal 尝试使用自定义验证器而不是使用范围验证器。请参考这个asp.net-tutorials.com/validation/custom-validator
  • 谢谢,会尝试,但是我们无法使用范围,因为范围内支持日期类型,我希望我的日期在特定范围内。
  • 我尝试了自定义验证器,但这里的问题是它只有在服务器端有回发时才会触发。我需要在选择日期时动态发生的事情,就像比较验证器的工作方式一样。
  • 尝试使用带有 Ajax 调用的自定义验证器。看看这里。 stackoverflow.com/questions/17162742/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-27
  • 2011-02-22
  • 1970-01-01
  • 1970-01-01
  • 2010-09-20
相关资源
最近更新 更多