3

I have added a <SharePoint:DateTimeControl> in my visual web part and I want to make it mandatory. To do so I referred this link and used as per my code.

My code is:

<tr>
    <td>
            <asp:Label ID="lblProjectPlannedEndDate" runat="server" Text="Planned End Date"></asp:Label>
    </td>
    <td>
            <SharePoint:DateTimeControl ID="dtcProjectPlannedEndDate" runat="server" DateOnly="true"/>

        <asp:RequiredFieldValidator ID ="rfvProjectPlannedEndDate" runat="server" ErrorMessage="Please Specfiy Project Planned End Date"
             ControlToValidate="dtcProjectPlannedEndDate$dtcProjectPlannedEndDate" ForeColor="Red" Display="Dynamic"></asp:RequiredFieldValidator>
    </td>
</tr>

I am getting error i.e. =Unable to find control id 'dtcProjectPlannedEndDate$dtcProjectPlannedEndDate' referenced by the 'ControlToValidate' property of 'rfvProjectPlannedEndDate'.

What should I do?

2
  • Tried as per your suggestion and its giving this error:=Unable to find control id 'dtcProjectPlannedEndDate$dtcProjectPlannedEndDate' referenced by the 'ControlToValidate' property of 'rfvProjectPlannedEndDate'.
    – users1100
    Oct 3, 2013 at 6:25
  • Now error occurs: Error=Control 'dtcProjectPlannedEndDate' referenced by the ControlToValidate property of 'rfvProjectPlannedEndDate' cannot be validated
    – users1100
    Oct 3, 2013 at 6:41

3 Answers 3

5

your code is fine appart from one part, you looping on the validation! you need to set date type at the end:

so you have:

ControlToValidate="dtcProjectPlannedEndDate$dtcProjectPlannedEndDate" 

should be:

ControlToValidate="dtcProjectPlannedEndDate$dtcProjectPlannedEndDateDate" 

i got that from the error you shown:

Unable to find control id 'dtcProjectPlannedEndDate$dtcProjectPlannedEndDate' referenced by the 'ControlToValidate' property of 'rfvProjectPlannedEndDate'.

so it got the first part correct but its after $ that it fails.

reading the error: its failing at ControlToValidate and more specifically dtcProjectPlannedEndDate$dtcProjectPlannedEndDate , tcProjectPlannedEndDate is correct as that is the correct field name, $ means to validate against tcProjectPlannedEndDate but missing the data type which in your case is (date) type!

full working code is:

<tr>
    <td>
            <asp:Label ID="lblProjectPlannedEndDate" runat="server" Text="Planned End Date"></asp:Label>
    </td>
    <td>
            <SharePoint:DateTimeControl ID="dtcProjectPlannedEndDate" runat="server" DateOnly="true"/>

        <asp:RequiredFieldValidator ID ="rfvProjectPlannedEndDate" runat="server" ErrorMessage="Please Specfiy Project Planned End Date"
             ControlToValidate="dtcProjectPlannedEndDate$dtcProjectPlannedEndDateDate" ForeColor="Red" Display="Dynamic"></asp:RequiredFieldValidator>
    </td>
</tr>

more info on control to validation and how it should look:

https://stackoverflow.com/questions/939802/date-validation-with-asp-net-validator

what i mean is your asking to validate a field name by a field name but missed the type which is date ;) in sudo it would look like

if ((field name == field name) = date(type) )
{
  //show nothing
}
else
{
  //show error 
}
4
  • Thank you so much sir, it worked perfectly. But I did not get where I was wrong? Will you please specify?
    – users1100
    Oct 3, 2013 at 9:30
  • what i mean is your asking to validate a field name by a field name but missed the type which is date ;) in sudo it would look like (field name == field name) = date(type)
    – Ali Jafer
    Oct 3, 2013 at 9:38
  • Thank you sir for explainig.. Sir, I am still working for this query. Did you get any idea?
    – users1100
    Oct 3, 2013 at 9:40
  • im at work atm and cant view anything from pastbin or any images posted via image hosting sites :(
    – Ali Jafer
    Oct 3, 2013 at 9:47
1

Try this way!!!!!!!!

    <sharepoint:datetimecontrol id="txtPrepDate" runat="server" dateonly="true">

         <asp:comparevalidator id="CompareValidator2" runat="server" forecolor="Red" controltovalidate="txtPrepDate$txtPrepDateDate" type="Date" operator="DataTypeCheck" errormessage="Please enter an valid date" display="Dynamic"></asp:comparevalidator>

    </sharepoint:datetimecontrol>

NOTE : Here ControlToValidate property you need to set as {DateControlID}${DateControlID}Date then only it will work otherwise it will through error

Also note operator = "DataTypeCheck"

http://kancharla-sharepoint.blogspot.co.uk/2012/11/sharepoint-datetimecontrol-validation.html

1
  • although this is a good answer please dont just copy off the net and not reference where you get it from!
    – Ali Jafer
    Oct 3, 2013 at 9:14
0
<SharePoint:DateTimeControl ID="dtcProjectPlannedEndDate" runat="server" DateOnly="true" IsRequiredField="true" />
<asp:RequiredFieldValidator ID ="rfvProjectPlannedEndDate" runat="server" ErrorMessage="Please Specfiy Project Planned End Date"
         ControlToValidate="dtcProjectPlannedEndDate$dtcProjectPlannedEndDateDate" ForeColor="Red" Display="Dynamic"></asp:RequiredFieldValidator>

Try the above!

Also, check there should be a property named IsRequiredField in SharePoint:DateTimeControl, make it true..

1
  • I tried the same way but not working. Throwing same error.
    – users1100
    Oct 3, 2013 at 7:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.