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

65,921

Solution 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.

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

Solution 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;
        }
    }
  }
Share:
65,921
HelloBD
Author by

HelloBD

Updated on March 31, 2020

Comments

  • HelloBD
    HelloBD about 4 years

    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.

  • HelloBD
    HelloBD over 14 years
    this show me The value '' of the ValueToCompare property of 'Comparevalidator1' cannot be converted to type 'Date'.
  • nuiun
    nuiun over 14 years
    Sorry - I forgot to mention that you'll also need to call "Page.DataBind()" in your Page_Load method.
  • comecme
    comecme over 12 years
    Just wondering, if this requires you to call DataBind() in the Page_Load, couldn't you just as well write MyCompareValidator.ValueToCompare = DateTime.Today.ToShortDateString(); in the Page_Load?
  • Jean-François Beauchamp
    Jean-François Beauchamp about 12 years
    The CompareValidator requires Currency, Date, Double, Integer or String as Type. Check with Intellisense in Visual Studio. I am using VS2008 and Asp.Net 3.5.
  • CallMeLaNN
    CallMeLaNN over 11 years
    You miss the Operator="GreaterThan" otherwise it will treat as Equal
  • LCJ
    LCJ almost 11 years
    @comecme I have posted an answer by setting ValueToCompare in code behind.
  • ErTR
    ErTR over 7 years
    Why do you have to use #? Is this a databinding operation?