EditDefault DateField's
In this case, the hack will default a date field to the value of another - this could be done as in-line javascript, but in this case it was being generated by a web part which is displayed a custom New/Edit form for a List.
EditHow it works
This hack was built for a very specific use case, it would need a little bit of tweaking to be generalized to work with multiple sets of fields in a single form, but it should give you the general idea.
The
sourceFieldName and
destinationFieldName should be the internal/static names for the fields in your ContentType that you're working with.
Just call the two methods (in order) prior to the page being rendered.
It will only copy the date, and works either when the picker has returned, or when on-blur fires for the source Element.
It takes advantage of the
g_strDateTimeControlIDs array which is maintained/generated by SharePoint itself for DateTime controls as in-line script in the page.
http://speechpaths.com/ speech writing help
EditThe code
private void RegisterCustomRequiredToInternalDefaultScript(string sourceFieldName, string destinationFieldName)
{
string script =
@"<_script type=""text/javascript"">
var sourceId = g_strDateTimeControlIDs][""SP" + sourceFieldName + @"""]];
var destinationId = g_strDateTimeControlIDs[[""SP" + destinationFieldName + @"""]];
var sourceElement = document.getElementById(sourceId);
var destinationElement = document.getElementById(destinationId);
function sourceDateOnBlur()
{
var pattern = new RegExp(""[0-3][0-9]-0|1[0-9]-19|20[0-9]{2}"");
if ((sourceElement.value) && (sourceElement.value.match(pattern)) && (!destinationElement.value))
{
destinationElement.value = sourceElement.value;
}
}
sourceElement.onblur = sourceDateOnBlur;
";
if (!Page.ClientScript.IsStartupScriptRegistered("sourceDateOnBlur"))
Page.ClientScript.RegisterStartupScript(this.GetType(), "sourceDateOnBlur", script);
}
private void OverrideClickDatePickerScriptToCauseBlur()
{
string script =
@"<_script type=""text/javascript"">
var oldClickDatePicker = clickDatePicker;
function newClickDatePicker(field, src, datestr)
{
oldClickDatePicker(field,src,datestr);
// add logic here to determine if the method should be called or not
sourceDateOnBlur();
}
clickDatePicker = newClickDatePicker;
";
if (!Page.ClientScript.IsStartupScriptRegistered("blurOnPickerClose"))
Page.ClientScript.RegisterStartupScript(this.GetType(), "blurOnPickerClose", script);
}