I'm using the SharePoint PeopleEditor to select a user and when the user is selected I want to populate a few fields with information about that user, I retrieve the data from AD. The problem I had was how do I make the PeopleEditor send a signal to my code that a value has been set?! There is no event on the PeopleEditor control which is fired when a value is set.
I first asked myself this question about a month ago, but as I have been unable to find an answer I added a button to my form which says "Retrieve Userinformation". This is not the normal way of UI design and during a demo for the customer today they made a remark about it, so I decided to take another look at this problem.
It took me almost an hour to realize that there is a property called AutoPostBack on some server controls. When you set this property a Postback will be generated when a value is set in the control. As you may understand from my lack of knowledge in this area ASP.NET UI design is not my primary competence, but in most cases I know enough to get the job done.
<SharePoint:PeopleEditor AutoPostBack="true" ID="peUser" runat="server" />
In the case of the PeopleEditor this means that when you type a username and click Check names (or hit enter) or use the Addressbook to select a user you will receive a generic postback, no specific event i raised by the page is reloaded. Therefore you can have code in your Page_Load which checks if a value has been selected and take some action.
string accountName = null;
if (peUser.ResolvedEntities.Count > 0)
PickerEntity entity = (PickerEntity)peUser.ResolvedEntities;
accountName = entity.Key;
int pos = accountName.IndexOf('\\');
accountName = accountName.Substring(pos + 1);
// take some action based on accountName
In essence the AutoPostBack acts as an event generator which lets you perform an action when the value of the control is set.
Assuming the user is returned in this format
domain\username you can do the following:
string userWithoutDomain = userName.Substring(userName.IndexOf('\\') + 1);
If the format is like this
username@domain then the following will work
string userWithoutDomain = user.Substring(0, user.IndexOf('@'));