Wednesday, December 31, 2014

Issue while applying validation to SharePoint Date Time Control

Hi Naga,


According to your post, you might want to use CompareValidator control to validate the SharePoint DateTime control.


The SharePoint DateTime control seems not work so well with the CompareValidator control.


As a workaround, you can implement the validation with an extra Label control like this:



<!--in the page-->

<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<SharePoint:DateTimeControl runat="server" Calendar="Gregorian" DateOnly="true" ID="ToDate" AutoPostBack="true" OnDateChanged="ToDate_OnDateChanged" />
<asp:Label ID="Label1" runat="server" Text="Label">date validation test</asp:Label>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="ToDate" EventName="DateChanged" />
</Triggers>
</asp:UpdatePanel>



//in code behind

protected void ToDate_OnDateChanged(object sender, EventArgs e)
{
if (ToDate != null)
{
Label1.Text += "<p/>" + ToDate.SelectedDate.ToShortDateString();
DateTime t1 = DateTime.Today;
DateTime t2 = ToDate.SelectedDate;
int i = DateTime.Compare(t1,t2);
if (i < 0)
{
//t1<t2
Label1.Text += " later than today";
}
if (i == 0)
{
//t1==t2
Label1.Text += " equal today";
}
if (i > 0)
{
//t1>t2
Label1.Text += " earlier than today";
}
}
}



It will work like this in a page:




Best regards




Patrick Liang

TechNet Community Support



No comments:

Post a Comment