使用 CompareValidator 控件将用户输入日期与今天的日期进行比较

发布于 2024-08-21 22:55:23 字数 560 浏览 4 评论 0原文

嘿..我想将当前日期与用户输入的日期进行比较..但是,到目前为止我遇到了错误..

我尝试了这样的操作:

<asp:TextBox id="txtDate1" runat="server" />    
<asp:CompareValidator runat="server" ErrorMessage="The date must be greater than today"
    ControlToValidate="txtDate1" type="date" 
    ValuetoCompare="DateTime.Today.ToShortDateString()" />

并且我收到一条错误,指出 DateTime 的值"" 的 ValueToCompare 属性的 .Today.ToShortDateString() 无法转换为“date”类型 我也尝试了 ValueToCompare="DateTime.Now.Date()" 并且收到了相同的错误消息。

请帮助我,我非常感激。

hey..i would like to compare the current date with the date entered by user..however, i'm encountering errors so far..

i tried something like this:

<asp:TextBox id="txtDate1" runat="server" />    
<asp:CompareValidator runat="server" ErrorMessage="The date must be greater than today"
    ControlToValidate="txtDate1" type="date" 
    ValuetoCompare="DateTime.Today.ToShortDateString()" />

and i got an error stating that the value of DateTime.Today.ToShortDateString() of the ValueToCompare property of "" cannot be converted to type 'date'
i also tried ValueToCompare="DateTime.Now.Date()" and i got the same error message.

please help me and i greatly appreciate it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

好菇凉咱不稀罕他 2024-08-28 22:55:24

试试这个。

<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ErrorMessage="Date is required" ControlToValidate="txtmDate"></asp:RequiredFieldValidator>

<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="Date is not valid (MM.DD.YYYY)" ControlToValidate="txtDate" ValidationExpression="(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d" ></asp:RegularExpressionValidator>

用户输入日期的方式(日期格式)也很重要。在这里,我使用了 MM.DD.YYYY 日期格式。

<asp:CompareValidator ID="CompareValidatorGreaterThanToday" runat="server" ErrorMessage="The date must be greater than today" ControlToValidate="txtDate" Type="date" Operator="GreaterThan" ValueToCompare="<%# DateTime.Today.ToShortDateString() %>" ></asp:CompareValidator>

然后在 Page_Load 方法 (*.aspx.cs) 中调用 Page.DataBind()。

例子:

protected void Page_Load(object sender, EventArgs e)
{            
   Page.DataBind();
}

Try this.

<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ErrorMessage="Date is required" ControlToValidate="txtmDate"></asp:RequiredFieldValidator>

<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="Date is not valid (MM.DD.YYYY)" ControlToValidate="txtDate" ValidationExpression="(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d" ></asp:RegularExpressionValidator>

The way user input the date (date format) is also Important. In here, I have used MM.DD.YYYY date format.

<asp:CompareValidator ID="CompareValidatorGreaterThanToday" runat="server" ErrorMessage="The date must be greater than today" ControlToValidate="txtDate" Type="date" Operator="GreaterThan" ValueToCompare="<%# DateTime.Today.ToShortDateString() %>" ></asp:CompareValidator>

Then in your Page_Load method (*.aspx.cs), call Page.DataBind().

Example:

protected void Page_Load(object sender, EventArgs e)
{            
   Page.DataBind();
}
初雪 2024-08-28 22:55:24

尝试下面写的:

<asp:CompareValidator runat="server" ErrorMessage="The date must be greater than today"
ControlToValidate="txtDate1" type="DateTime" 
ValuetoCompare='<%# DateTime.Now.ToString("d") '%> />

Try the below written :

<asp:CompareValidator runat="server" ErrorMessage="The date must be greater than today"
ControlToValidate="txtDate1" type="DateTime" 
ValuetoCompare='<%# DateTime.Now.ToString("d") '%> />
玩物 2024-08-28 22:55:23

您只是将 ValueToCompare 属性用作文字字符串。如果要执行代码来获取动态值,则需要在其中使用 ASP 标记。试试这个:

<asp:comparevalidator runat="server" 
  errormessage="The date must be greater than today"
  controltovalidate="txtDate1" type="date" 
  valuetocompare="<%# DateTime.Today.ToShortDateString() %>" />

然后在您的 Page_Load 方法中,调用 Page.DataBind()

这将在页面加载时执行数据绑定器代码,并将值放在引号之间。

You're just using the ValueToCompare property as a literal string. You need to use ASP tags in it if you want to execute code to get a dynamic value. Try this:

<asp:comparevalidator runat="server" 
  errormessage="The date must be greater than today"
  controltovalidate="txtDate1" type="date" 
  valuetocompare="<%# DateTime.Today.ToShortDateString() %>" />

Then in your Page_Load method, call Page.DataBind().

This will execute the databinder code when the page is loaded, and put the value in between the quotes.

痴情换悲伤 2024-08-28 22:55:23
    <asp:CompareValidator ID="CompareValidator3" runat="server" 
                        ControlToValidate="TextBox1" ErrorMessage="Date should be on or after today" 
                        Operator="GreaterThanEqual" Type="Date">
</asp:CompareValidator>

在页面加载事件中设置验证器的值进行比较

CompareValidator3.ValueToCompare = DateTime.Now.ToShortDateString();
    <asp:CompareValidator ID="CompareValidator3" runat="server" 
                        ControlToValidate="TextBox1" ErrorMessage="Date should be on or after today" 
                        Operator="GreaterThanEqual" Type="Date">
</asp:CompareValidator>

In the page load event set the validator's value to compare as

CompareValidator3.ValueToCompare = DateTime.Now.ToShortDateString();
泛泛之交 2024-08-28 22:55:23

代码后面设置ValueToCompare

        if (!Page.IsPostBack)
        {
           string currentDate = DateTime.Today.ToShortDateString();
           Comparevalidator1.ValueToCompare = currentDate;
        }

我们可以在比较验证器的

    <asp:CompareValidator ID="Comparevalidator1" runat="server" ErrorMessage="The date must be greater than today"
    Operator="GreaterThan" ControlToValidate="txtDate1" Type="date" Display="Dynamic" />

为什么不使用Page.DataBind?

考虑以下场景。我只需要在单击“操作”按钮时显示网格视图。数据源以声明方式定义。但是,如果我使用 Page.DataBind(),即使在页面加载时它也会显示网格。

 <form id="form1" runat="server">
 <asp:TextBox ID="txtDate1" CssClass="firstBox" runat="server" Text=""></asp:TextBox>
 <asp:CompareValidator ID="Comparevalidator1" runat="server" ErrorMessage="The date must be greater than today"
    Operator="GreaterThan" ControlToValidate="txtDate1" Type="date" Display="Dynamic" />
 <asp:Button ID="btnAction" class="submitButton" runat="server" Text="Action" OnClick="btnAction_Click" />
 <asp:Button ID="btnDummy" class="submitButton" runat="server" Text="Dummy" OnClick="btnDummy_Click" />
 <br />
 <br />

 <asp:GridView ID="GridView1" runat="server" DataSource="<%# EmployeesResult %>">
 </asp:GridView>
 </form>

代码隐藏

public partial class ThirdTest : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //Page.DataBind();

        if (!Page.IsPostBack)
        {
           string currentDate = DateTime.Today.ToShortDateString();
           txtDate1.Text = currentDate;
           Comparevalidator1.ValueToCompare = currentDate;
        }
    }

    protected void btnAction_Click(object sender, EventArgs e)
    {
        GridView1.DataBind();
        string value = GridView1.DataSource.ToString();
    }

    protected void btnDummy_Click(object sender, EventArgs e)
    {

    }

    //Propertry
    public List<Employee> EmployeesResult
    {
        get
        {
            List<Employee> employees = new List<Employee>();
            employees.Add(new Employee { EmpID = 1, EmpName = "Emp1" });
            employees.Add(new Employee { EmpID = 2, EmpName = "Emp2" });
            return employees;
        }
    }
  }

We can set the ValueToCompare in code behind

        if (!Page.IsPostBack)
        {
           string currentDate = DateTime.Today.ToShortDateString();
           Comparevalidator1.ValueToCompare = currentDate;
        }

for the compare validator:

    <asp:CompareValidator ID="Comparevalidator1" runat="server" ErrorMessage="The date must be greater than today"
    Operator="GreaterThan" ControlToValidate="txtDate1" Type="date" Display="Dynamic" />

Why not use Page.DataBind?

Consider the following scenario. I need to display the gridview only on the click of the Action button. The datasource is defined declaratively. But, if I use Page.DataBind() it will show the grid even on the page load.

 <form id="form1" runat="server">
 <asp:TextBox ID="txtDate1" CssClass="firstBox" runat="server" Text=""></asp:TextBox>
 <asp:CompareValidator ID="Comparevalidator1" runat="server" ErrorMessage="The date must be greater than today"
    Operator="GreaterThan" ControlToValidate="txtDate1" Type="date" Display="Dynamic" />
 <asp:Button ID="btnAction" class="submitButton" runat="server" Text="Action" OnClick="btnAction_Click" />
 <asp:Button ID="btnDummy" class="submitButton" runat="server" Text="Dummy" OnClick="btnDummy_Click" />
 <br />
 <br />

 <asp:GridView ID="GridView1" runat="server" DataSource="<%# EmployeesResult %>">
 </asp:GridView>
 </form>

Code behind

public partial class ThirdTest : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //Page.DataBind();

        if (!Page.IsPostBack)
        {
           string currentDate = DateTime.Today.ToShortDateString();
           txtDate1.Text = currentDate;
           Comparevalidator1.ValueToCompare = currentDate;
        }
    }

    protected void btnAction_Click(object sender, EventArgs e)
    {
        GridView1.DataBind();
        string value = GridView1.DataSource.ToString();
    }

    protected void btnDummy_Click(object sender, EventArgs e)
    {

    }

    //Propertry
    public List<Employee> EmployeesResult
    {
        get
        {
            List<Employee> employees = new List<Employee>();
            employees.Add(new Employee { EmpID = 1, EmpName = "Emp1" });
            employees.Add(new Employee { EmpID = 2, EmpName = "Emp2" });
            return employees;
        }
    }
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文