Archive for February, 2008

Asp.Net Validators Incompatible with SharePoint controls

On a recent project I learned the hard way that the Asp.Net validator controls like RequiredFieldValidator or RangeValidator do not work with the Microsoft.SharePoint.WebControls controls. For this reason you will frequently need to create your own validation logic when working with controls from this SharePoint namespace. When using Reflector to inspect the type of the ControlToValidate field used in the Asp.Net validators, it appears that it is looking for a control of type System.Web.UI.WebControl. Interestingly, in Reflector the DateTimeControl appears to have its own RequiredFieldValidator in its child controls that is enabled when the IsRequiredField="true" attribute is set. It is also important to note that SharePoint also provides some validators within the Microsoft.SharePoint.WebControls namespace including the InputFormRequiredFieldValidator or InputFormRangeValidator, but even these are incompatible with some of the other SharePoint controls.
 
So an important question that comes up is what is the best way to handle validation on SharePoint controls. A couple of different approaches can be taken. One would be to derive from the existing validator controls and to expect that a control of type Microsoft.SharePoint.WebControls.SPControl. Another approach would be to emulate the functionality of the System.Web.UI.WebControls behavior by using a label and then calling the validation logic. The primary gotcha that was encountered with this second approach was that the convenience of calling Page.Validate() will unfortunately invoke the built-in SharePoint control validation which may or may not fulfill your application requirements.
Advertisements

,

3 Comments

Custom DateTimeControl Formatting

A problem that I recently encountered working on a MOSS project was the inability to customize date formating of the Microsoft.SharePoint.WebControls.DateTimeControl for a setting that is outside the realm of the default formatting for a culture. The default culture short date formatting was dd/mm/yyyy for en-AU but instead I needed it to display in dd MMM yyyy. This control has a LocaleId property but no way of simply customizing the formatting of the selected date value.
 
All I could find on the net that would work was to redefine the culture setting in the registry using the CultureAndRegionInfoBuilder (as mentioned here: http://sharepointex.blogspot.com/2007/09/how-to-modify-date-format-in-sharepoint.html). This was not ok for me because I was deploying into a shared environment and this would break someone else’s culture-sensitive formatting. So I created a derived class from the DateTimeControl and overrode the OnPreRender event so that I could handle the formatting myself and this did the trick. Here is my code for doing so:
 

using System;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
namespace TestDerivedControl
{
    [Guid("619b97bb-1c70-426e-b3ec-f76c5324be3a")]
    TestDerivedControl : System.Web.UI.WebControls.WebParts.WebPart
    {
        public TestDerivedControl()
        {
            this.ExportMode = WebPartExportMode.All;
        }
        protected override void Render(HtmlTextWriter writer)
        {
            this.RenderChildren(writer);
        }
       
        protected override void CreateChildControls()
        {
            base.CreateChildControls();
            // Add the controls here
            CustomAUDatePicker obj = new CustomAUDatePicker();
           
            obj.ID = "dtcDate";
            obj.AutoPostBack = true;
            obj.DateOnly = true;
            obj.Visible = true;
            obj.SelectedDate = DateTimeNow;
            this.Controls.Add(obj);
        }
    }
    public class CustomAUDatePicker : Microsoft.SharePoint.WebControls.DateTimeControl
    {
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            TextBox box = (TextBox)this.Controls[0];
            DateTime dt = DateTime.Parse(box.Text);
            box.Text = dt.ToString("dd MMM yyyy");
       
        }
          
    }
}
 

,

6 Comments

Microsoft Bids on Yahoo

Merger mania is definitely back! What exciting news to find out that Microsoft is offering to buy Yahoo for $45 billion! The social networking universe is getting a lot smaller with this news which will create some very interesting new products. With the yahoo capabilities under the Microsoft banner I bet Windows Live will be getting a makeover and a considerable list of new features. I can just imagine being able to link Facebook groups to Yahoo groups to Windows Live groups so you can be a part of any one of them and have roaming profiles all along the way.
 
A huge question from the business side is how the Yahoo brand will be preserved. With Yahoo owning so many diverse companies it will be very interesting to see the merging of the Windows Live marketplaces and the Yahoo marketplaces to create much more competitive areas to compete with eBay.

Leave a comment