mistermuggles
mistermuggles

Reputation:

DateTimeControl in Sharepoint (c#)

I'm trying to change the default values in the "time" drop down list that the DateTimeControl displays in Sharepoint. I want an increment of 15, not 5 minutes. Anybody has any idea how this could be done? Can I overload a method or something?

Upvotes: 2

Views: 5957

Answers (4)

Pooja
Pooja

Reputation: 11

You can get the value of selected Hour and Minute from the SharePoint:DateTimeControl in C# by the following code:

DateTimeControlName.SelectedDate.Hour 

&

DateTimeControlName.SelectedDate.Minute. 

These statements will return the hour and minute in Integer format.

I know this post is quite old. But I just thought this may help someone coming to this post.

Upvotes: 1

Mhd. Yasseen
Mhd. Yasseen

Reputation: 1037

Go to

[12]\TEMPLATE\LAYOUTS\1033\BFORM.JS

In line : 7690

Change: this.dminControl=5; to this.dminControl=15;

It works, but all DateTimePickers will have an increment to 15 minutes

Upvotes: 2

Faheem
Faheem

Reputation: 3569

As a matter of fact both the time drop down and its initializers are implemented as private data members of the DateTimeControl class so you can not change the values directly. However, the minutes drop down is prepared inside OnPreRender, we can get the control and reset its values indirectly to get desired behavior. Here is one approach

public class MyDateTimeControl : DateTimeControl
{
    protected override void Render(HtmlTextWriter output)
    {
        DropDownList minuteControl = null;
        string[] newMinutesRange = new string[] { "00", "15", "30", "45" };
        string[] newMinutesRangeExt = new string[] { "00", "15", "30", "45", "" };
        int index = 0;
        int selectedMinutes;

        try
        {
            if (!this.DateOnly && this.Controls.Count == 4)
            {
                minuteControl = (DropDownList)this.Controls[2];
            }
        }
        catch { }

        if (minuteControl != null && !this.DateOnly)
        {
            selectedMinutes = Convert.ToInt32(minuteControl.SelectedValue);
            if (selectedMinutes % 15 > 0)
            {
                index = 4;
                newMinutesRangeExt.SetValue(selectedMinutes.ToString(), index);
                newMinutesRange = newMinutesRangeExt;
            }
            else
            {
                index = selectedMinutes / 15;
            }

            minuteControl.Items.Clear();
            minuteControl.SelectedIndex = 0;
            minuteControl.DataSource = newMinutesRange;
            minuteControl.DataBind();
            minuteControl.SelectedIndex = index;
        }

        base.Render(output);            
    }
}

Hope this helps

Upvotes: 2

Mark
Mark

Reputation: 1449

Unfortunately this is not possible using the out of the box DateTime field.

A SharePoint field is made up of 2 main parts. The data structure (in code) and the various views (namely in a list, new/edit/view, admin [when adding to a list]). The data structure out of the box is a standard .NET DateTime field. Unfortunately the views only give the increment by 5 minutes.

You can create your own by inheriting from the default field. MSDN has a decent explaination of how. Nick Sevens has a much clearer explanation.

Unfortunately (as with most SharePoint customizations) creating your own field in CAML can be tricky.

This project on CodePlex might be a good starting point. It's licensed under the GPL so you can modify it.

Upvotes: 1

Related Questions