Sharepoint Forum

Ask Question   UnAnswered
Home » Forum » Sharepoint       RSS Feeds

datetimecontrol in a webpart, validating its there and its a valid

  Asked By: Darren    Date: Dec 28    Category: Sharepoint    Views: 5127

I have a sharepoint datetimecontrol in a webpart that i am creating
in C#, this sits alongside some other text boxes. When I save the
form i want to check that a) the user has selected a date and b) the
date is valid. Im using wss 3.0.

eg. I create the datetimecontrol as follows

_lblStartDate = new Label();
_lblBeginDate.ID = "lblBeginDate";
_lblBeginDate.CssClass = "amx-addeditmodel-fieldlabel";
_lblBeginDate.Text = WebPartsResources.amxFieldLabelBeginDate;
_dtcBeginDate = new DateTimeControl();
_dtcBeginDate.ID = "dtcBeginDate";
_dtcBeginDate.DateOnly = true;
_dtcBeginDate.IsRequiredField = true;
_dtcBeginDate.ErrorMessage = "ENTER A DATE!";

Now, being quite new to all this I thought I could add an asp
required field validator to the control and possibly a range
validator. I read somewhere you cannot do this for date controls
(however is this just the calendar control i.e. not the
datetimecontrol which is sharepoint specific). So why not try it you
say? Well i had enough problems on my page without knowing if this
was one of them!
Q1. Is this the case?

Q2) If I dont add a validator i thought I could just call validate()
when saving the form and as i set its IsRequiredField to true, and
then I did not set date it would fail and show the errormessage but
it was saying the datecontrol was valid even though there was no
date. Ive probably got the wrong end of the stick on this.

Q3_ I looked in the sdk and to be fair i found it pretty useless,
i.e. no examples in code and no description of what each property
actually meant, thus any help greatly appreciated



3 Answers Found

Answer #1    Answered By: Lynn Mann     Answered On: Dec 28

Validator controls work the same in WSS as they do in any .NET
application. I don't know if Validator controls can be used for calendar
controls or not.

Answer #2    Answered By: Damini Dande     Answered On: Dec 28

I don't think you can use the validator controls with the DateTime controls
in SharePoint. What you should probably do instead is add a handler to the
Validating (Not Validated) event of the DateTime control. Then you can test
for the required results and set the Cancel property of the EventHandler
argument to True if the field is left blank. An error message should also
be displayed. I don't have my SharePoint VPC handy, but I can drop a short
example into the list for you tomorrow.

Answer #3    Answered By: Irving Hurley     Answered On: Dec 28

Just a further followup to my comments about your problem yesterday. Have
you tried using the .IsReguiredField property of the SharePoint
dateTimeControl class? I tried it yesterday and although it doesn't check
for the validity of the date  it does verify that a date has been entered in
the same way that the RequiredValidator control would. If you also set the
MinDate and MaxDate property it will check  for a valid  date value. However,
when I set the Min and Max dates it didn't check consistently unless a
partial date had been entered. So listed below is the code I would suggest
you use.

DateTimeControl myTest = new DateTimeControl();

myTest.ID = "DateTime";

myTest.IsRequiredField = true;

myTest.MinDate = System.DateTime.Parse( "01/01/1900");

myTest.MaxDate = System.DateTime.Parse("12/31/2025");

myTest.SelectedDate = System.DateTime.Now;