Click here to Skip to main content
15,881,803 members
Articles / Web Development / ASP.NET

Utilizing SharePoint Form Controls (like OWSDateField)

Rate me:
Please Sign up or sign in to vote.
4.56/5 (6 votes)
3 Jan 20063 min read 66.4K   216   34  
This article discusses some ways to take advantage of the SharePoint client side controls (like the OWSDate Control) within the SharePoint web parts.
using System;
using System.Text;
using System.Web.UI;

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.Utilities;

namespace OWSControls
{
    public class OWSDateField : OWSBase
    {
        #region construction

        /// <summary>
        /// Default constructor
        /// </summary>
        public OWSDateField() { }

        #endregion

        #region custom properties

        /// <summary>
        /// Gets or set the number date in a .NET date time format. 
        /// </summary>
        public DateTime DateTime
        {
            get { return SPUtility.CreateSystemDateTimeFromXmlDataDateTimeFormat(Value); }
            set { Value = SPUtility.CreateISO8601DateTimeFromSystemDateTime(value); }
        }

        /// <summary>
        /// Gets or set whether or not this control presents a date only value.
        /// </summary>
        public bool DateOnly
        {
            get
            {
                if (ViewState["DateOnly"] == null)
                    DateOnly = false;
                return (bool)ViewState["DateOnly"];
            }
            set { ViewState["DateOnly"] = value; }
        }

        /// <summary>
        /// Gets or set whether or not to hide descriptions.
        /// </summary>
        public bool HideDescription
        {
            get
            {
                if (ViewState["HideDescription"] == null)
                    HideDescription = false;
                return (bool)ViewState["HideDescription"];
            }
            set { ViewState["HideDescription"] = value; }
        }

        #endregion

        #region overridden properties

        /// <summary>
        /// Gets the test string representation of the date.
        /// </summary>
        public override string Text
        {
            get 
            { 
                if (DateOnly)
                    return DateTime.ToString("dddd, MMMM d, yyyy");
                return DateTime.ToString("dddd, MMMM d, yyyy h:mm tt");
            } 
        }

        #endregion

        #region overridden methods

        /// <summary>
        /// Renders the OWS Control
        /// </summary>
        /// <param name="wtr">writes out the text control information</param>
        protected override void Render(HtmlTextWriter wtr)
        {
            wtr.Write(
                "<SCRIPT>" +
				"fld=new DateField(frm, '{0}', '{1}', '{2}'); " +
				"fld.fDateOnly = {3}; " +
				"fld.fHideDescription = {4}; " +
				"fld.caltype = 1; " +
				"fld.fRequired = {5}; " +
				"fld.BuildUI(); " +
                "</SCRIPT>",
                UniqueID,
                Display,
                JavaScriptValue,
                DateOnly.ToString().ToLower(),
                HideDescription.ToString().ToLower(),
                Required.ToString().ToLower()
            );
        }
        #endregion

    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions