ASP.NET 2.0 CompareValidator Part 2 - ControlToValidate/ControlToCompare do not work in MasterPage
This is a part 2 of the earlier one. Why there is a part 2? You will know it soon. Before you read this, I will recommend you to read my 1st post first http://community.sgdotnet.org/blogs/chuawenching/archive/2006/10/30/ASP.NET-2.0-CompareValidator-_2D00_-Compare-Dates-through-custom-user-controls.aspx
I will like to say thanks to Alvin (http://alvinzc.blogspot.com/) for helping me out to figure out this one. Something that I don't really know how to fix. Cool buddy :)
Like I said earlier you need to pass a unique id to the parent web form's compare validator in order to get it work. But there is one big problem.
What if you have a web form which uses a Master Page? I can tell you this; it will not work at all using my earlier technique. Not for master pages.
There are 2 issues here, the 1st one is the common and the 2nd one happens to migrated .NET 1.0/1.1 to 2.0 user controls:
Common
i) Master Page
Without Master Page, it will give you this:
uclCalendar$textBox
With Master Page, it will give you this:
_ctl$ContentPlaceHolder$UclABC_GeneralInformation$uclABC_GeneralInformationDetail$
uclCalendar$txtDateValue
Why should I know this? How does it make any differences in this scenario? You should read this link http://www.odetocode.com/Articles/450.aspx under "FindControl, Javascript and Naming Containers"
Extracted from that link "FindControl only searches inside the current naming container. Using the FindControl method on the Page reference means we won’t be searching inside of MasterPage control. "
By using this way, you will get error like this "Unable to find control id ..."
Uncommon
ii) I had this user control ported by my ex colleague. It was a calendar control with javascript/vbscript. Complicated control which I didn't really understand the code. J
However I try to compare the codes with .NET 2.0 user controls. Looks okay, like inherit from the same Web.UI.UserControl. No special code, just extra html/javascript rendering and calling vbscripts.
When you try to do like the first way (read my 1st post on this) by passing the unique id, instead of giving you this:
*If you not sure about INamingContainer, you can read it at Alvin's blog or the internet
Container$UserControl$TextBox
But it gives you this:
Container:UserControl:TextBox
Don't ask me why? Coz I really not sure what happen? It is the same code implementation like a normal .NET 2.0 web user control.
I had tried a number of solutions:
a) CompareValidator.ControlToValidate = (uclCalendarStartDate.FindControl("txtDateValue") as TextBox).UniqueID;
b) I tried to create a property and using string.replace the character ‘:’ to ‘$’. But still failed.
c) I tried to create instance of ContentPane, and slowly from container down to specific controls
In order to solve this, try this code then. Take note; only use this code if you are facing similar problems like me as I am using String.Format. But it will work in Master Pages for sure. J
///////////////////////////////////////////
// Functional Code
// *Thanks to Alvin
TextBox startTextBox = uclCalendarStartDate.FindControl("txtDateValue") as TextBox;
cmpDate.ControlToCompare = String.Format("{0}${1}", uclCalendarStartDate.ID, startTextBox.ID);
TextBox endTextBox = uclCalendarEndDate.FindControl("txtDateValue") as TextBox;
cmpDate.ControlToValidate = String.Format("{0}${1}", uclCalendarEndDate.ID, endTextBox.ID);
//
///////////////////////////////////////////
If you are not facing the uncommon problem just do this instead:
cmpDate.ControlToCompare = uclCalendarStartDate.ID + “$” + startTextBox.ID);
Yes my problem is solved now. Cool J Just to share with you all if you face this problem.