Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Compare Validator is Unable to find control 1

Status
Not open for further replies.

NuJoizey

MIS
Aug 16, 2006
450
US
I created a custom textbox control that has a textbox for the user to enter a date, but when the user clicks on the textbox, a calendar picker pops up. It lives in a ascx file, and it supposed to be re-usable - right?

Ok, so now I drop two of them on my aspx page so that the user can pick a "from date" and a "to date". But I need to validate so that the user can't pick something like:

From: 4/16/08 To: 3/16/07

The problem is that ASP.NET I does not find them, throwing the error Unable to find control id 'tctl00$cphMenu3$calFromDate$txtFromDate' referenced by the 'ControlToValidate' property of 'cvCheckDates'.

Here is my aspx page:
Code:
                    <div>
                        <asp:ScriptManager id="scrmanScriptManager" runat="server"/> 
                        <AJAXExtensions:UpdatePanel ID="upanCalendarPicker" runat="server">
                            <ContentTemplate>
                                <slcCustomControl:DVDatePicker id="calFromDate" runat="server"/>
                                <slcCustomControl:DVDatePicker id="calToDate" runat="server"/>
                                <asp:CheckBox ID="chkAny" runat="server" Checked="True" ToolTip="Chooses All Dates" AutoPostBack="True" OnCheckedChanged="chkAny_CheckedChanged" />                                                                 
                                <asp:CompareValidator ID="cvCheckDates" runat="server" ErrorMessage="You Doofus">You Doofus</asp:CompareValidator>
                            </ContentTemplate>
                        </AJAXExtensions:UpdatePanel>
                    </div>

and in my VB, I'm trying to do this:
Code:
        Dim txtFromDate As TextBox = Me.calFromDate.FindControl("txtFromDate")
        Dim txtToDate As TextBox = Me.calToDate.FindControl("txtFromDate")

Dim cv As CompareValidator = Me.cvCheckDates

        cv.ControlToValidate = txtToDate.ClientID
        cv.ControlToCompare = txtFromDate.ClientID


        cv.Type = ValidationDataType.Date
        cv.Operator = ValidationCompareOperator.GreaterThan

which worked fine when I added onClick attributes for javascript - but what in blazes is the deal with the compare validator? i've searched around and seen some posts that say that this is not easily possible, that can't be true can it? If so then is the workaround having to write a JavaScript data validator script from scratch?


 
I think jbenson is right. I've never had to use a ClientID of a control except directly in javascript events/code.
 
Hi everyone, thanks for your replies.

I had tried that. no dice. I also tried ".uniqueID" and I still get

Unable to find control id 'txtFromDate' referenced by the 'ControlToCompare' property of 'cvCheckDates'.

Am I in the wrong event handler? I'm trying to do this at Page_Load, I went and tried some of the other events, but the error persists.
 
why are you doing this?:
Dim cv As CompareValidator = Me.cvCheckDates
i assume that cvCheckedDates is a compare validator you dropped onto your page
 
because I plopped an asp CompareValidator control on the page and I need to refer to it programatically in order to try to set the ControlToValidate, ControlToCompare, Type and Operator attributes.

I see no way to refer to these controls directly in the aspx page, since these controls don't actually exist there, they are in the ascx.

Does that make sense? Is this not the correct approach?

Seems to me there has to be some sort of way to validate at the page level, since there are two separate instances of my calendar control on my aspx - I don't see how you could put the validation anywhere else?
 
I am confused. Explain where the controls are. I thought the compare validator was on the aspx page containg the user controls, correct? If that is the case then you can just refernce the validator directly.
 
the controls were created as an ascx file, (a boilerplate as I understand it), so that I could re-use it (supposedly)easily. They are referenced in my aspx pages as shown in my first post. (hence the tag slcCustomControl:DVDatePicker)

Since it exists in the ascx, the controls that make up my custom date picker are not explicitly exposed on the aspx page. So for example, one of the controls that needs to be validated is txtFromDate, which is part of the custom control, but resides in the ascx file, but does not exist explicity on the aspx page. So you can't reference that txtFromDate directly in the validator which resides in the aspx file, or it doesn't know what you are talking about.
 
I didn't have time to create these controls as you have. But I would suggest you put the to and from and validator controls on ONE user control. I can't see an instance when you would just want a to or just a from date. If you do need that functionality, then you can simply create properties on your UC. In the aspx you can set the properties to enable, disable controls, or make them visible or not depending on what you need.
 
yes, the more I think about it, the more i think you are right, and I came to the same conclusion while driving home today. At least that's somewhat of a comforting thought. I made the custom control with just one textbox and one popup calendar thinking that this was the basic building block for something more complex - like what would happen if I wanted to combine just one calendar popup with say, an array of checkbox, or a combo box etc... Then I would not want to have the second popup calendar control.... I was looking for more freedom to mix and match

.... but it seems that you just can't do validation on custom controls very easily unless all of the controls reside inside the native ascx file.

I will noodle with this tomorrow and post back my results.

Thanks for helping me think about this.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top