Like many people that develop SharePoint Application, I use the PeopleEditor user control. Nice little control that you can put on the page if you want to be able to select people\groups just like it does in SharePoint pages.
Recently I had a web form that had a control (PeopleEditor) for closed by and a corresponding textbox on it was closed date. What I wanted is that when the Closed By value changed the Closed Date textbox would default to the current date. The OnValueChangedClientScript event worked to an extent. It was truly on the value though but if you were to delete the value the script did not fire. If the value was not resolved right away then the script did not run, so in short it did not run until a value was resolved. I should point out as I recall the script reacted as I wanted in FireFox. All my SP Users are on IE; if you are wondering why I use FireFox when all my users are IE, I do it for client side debugging with FireBug.
I created a solution with jQuery and the biggest thing I really don’t like about it is that I had to hard code server control ids, because I was unable to find the property that would give it to me in the <%= %>. Right or wrong this is what I did, I am sure there are other ways of doing it.
The first thing I did was put a blur event on the People Editor control. Since I have many of these I made a function for reusability on setting the date (BTW pp stands for People Picker, and it sounds funny). Most of this is pretty basic but I got stuck with the GetPPValue function.
$(document).ready(function() {
$("#ctl00_PlaceHolderMain_ppResolvedBy_upLevelDiv").blur(function() {
ppChangeUpdateDate($("#ctl00_PlaceHolderMain_ ppResolvedBy_upLevelDiv"), $("#<%= txtResolvedDate.ClientID %>"));
});
});
This function just simply checks the length and then updates the Date Text Box.
function ppChangeUpdateDate(ppThatChange, dateToUpdate) {
var ppValue = GetPPValue(ppThatChange);
if (ppValue.length > 0) {
setDateField(dateToUpdate);
}
else {
dateToUpdate.val("");
}
}
The get pp value function probably looks a bit strange but where the value is stored is different deppending if the name is resolved or not. I just exit at the first chance I get when I find something.
function GetPPValue(PPObject) {
// sharepoint stroes the value as a attr if it is resolved
var ppDivAttr = PPObject.find("span").find("div").attr("displaytext");
// sharepoint saves the value as div text if it is not resolved.
var ppDivText = PPObject.text();
if (ppDivAttr != null) {
if (ppDivAttr.length > 0) {
return ppDivAttr;
}
}
if (ppDivText.length > 0) {
return ppDivText;
}
return "";
}
Of course a function that sets the date if there is not already one there.
function setDateField(fieldToSet) {
var dt = new Date();
if (fieldToSet.val().length == 0) {
fieldToSet.val(dt.getMonth() + 1 + "/" + dt.getDate() + "/" + dt.getFullYear());
}
}