Using the CompareValidator control to compare user input date with today's date

Deadly 提交于 2019-12-10 12:36:00

问题


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.


回答1:


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.




回答2:


    <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();



回答3:


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;
        }
    }
  }



回答4:


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();
}



回答5:


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") '%> />


来源:https://stackoverflow.com/questions/2264215/using-the-comparevalidator-control-to-compare-user-input-date-with-todays-date

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!