“Hocus Focus” some magic spells … ok, my English is bad
I will like to share with you on what I find out on this. If you already know what ASP.NET 2.0 CompareValdator control is, just ignore the top part and read the bottom part. I hope it will help people.
ASP.NET 2.0 CompareValidator is quite a common control used by developers to validate input controls. I think the most common scenario is date comparisons. Assuming you have 2 text boxes, one start date and the other one end date. How do you check whether the end date more than the start dates?
You can achieve very easily without any extra tweaking like below:
<asp:CompareValidator ID="CompareValidator1" runat="server" ErrorMessage="CompareValidator" Operator="GreaterThan"
Type="Date" ControlToCompare="txtStartDate" ControlToValidate="txtEndDate"></asp:CompareValidator>
There are a few key areas here that you need to take note on.
Attribute | Description |
ErrorMessage | Your error description if there is an error triggered |
Operator | There are a few types, equal, greaterthan, greater than and equal, datatypecheck |
Type | Date, String (for my case I will choose Date) |
ControlToCompare | Since I am comparing the end date greater than the start date, so ControlToValidate=EndDate and ControlToCompare=StartDate |
ControlToValidate |
In order to use ControlToCompare and ControlToValidate property, your input controls must be within the web form itself.
Now I have a new scenario. Assuming this:
You have user controls. 1 is for Start Date and 1 is for End Date. Within the user control, there are 1 text box and a button (which triggers a javascript calendar; pass the values to the textbox).
Drag these 2 user controls into a web form (default.aspx).
Try with the code above. Of course you won’t get it work, since there are no longer text boxes in the same form, but user controls instead. The question is how do you pass to ControlToCompare and ControlToValidate?
I had tried a lot of ways, like creating properties, played with ValidatationPageAttribute (something like that), passing ClientID, passing ID … just can’t work.
Then I found out a way. Try this in your page_load, and it will work J
protected void Page_Load(object sender, EventArgs e)
{
// Take note, passing UniqueID is very important
// Not ClientID or ID, it won't work
CompareValidator1.ControlToValidate = uclDate1.FindControl("txtEndDate").UniqueID;
CompareValidator1.ControlToCompare = uclDate.FindControl("txtStartDate").UniqueID;
}
You have to pass in the UniqueID and not others. Very important here.
I had placed the source code as below for your personal understanding. I do not have access to sgdotnet site to upload files L
// Default.aspx //
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register Src="Date2.ascx" TagName="Date2" TagPrefix="uc2" %>
<%@ Register Src="Date1.ascx" TagName="Date1" TagPrefix="uc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Testing Compare Validators with User Controls</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<uc1:Date1 ID="uclDate" runat="server"></uc1:Date1>
<br />
<br />
<br />
<uc2:Date2 ID="uclDate1" runat="server" />
<br />
<br />
<asp:CompareValidator ID="CompareValidator1" runat="server" ErrorMessage="CompareValidator"
Operator="GreaterThan" Type="Date" ControlToCompare="txtStartDate" ControlToValidate="txtEndDate"></asp:CompareValidator>
<asp:Button ID="Button1" runat="server" Text="Button" /></div>
</form>
</body>
</html>
// Default.aspx.cs //
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// Take note, passing UniqueID is very important
// Not ClientID or ID, it won't work
CompareValidator1.ControlToValidate = uclDate1.FindControl("txtEndDate").UniqueID;
CompareValidator1.ControlToCompare = uclDate.FindControl("txtStartDate").UniqueID;
}
}
// Date1.ascx //
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="Date1.ascx.cs" Inherits="Date1" %>
Start Date <asp:TextBox id="txtStartDate" runat="server"></asp:TextBox>
// Date2.ascx //
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="Date2.ascx.cs" Inherits="Date2" %>
End Date <asp:TextBox id="txtEndDate" runat="server"></asp:TextBox>
Note: There is still one problem about CompareValidator. Take note, when you specify the type as Date, it actually checks for dd/mm/yyyy. If your textbox display as dd-MMM-yyyy or 10-October-2006, it won’t validate. I had checked online and it seemed that you had to build your own custom validator for this. If anyone knows how I can achieve this in CompareValidator, let me know yeah J
Thanks.