Pages

Monday, October 31, 2011

How to find a control in a DataList?


How to find a control in a DataList?

Because DataList is databound control which repeats its item template as many times as there are rows in the data source. Therefore contents of
So, to locate a specific Label control, you’d first need to locate a specific dataList item, and run FindControl to it (for example DataList1.Items(0).FindControl(“label2″) ), and considering the databound nature, you’d probably want to loop through all items in the DataList’s Items collection to get to the every Label control.

VB
For Each dli as DataListItem in DataList1.Items
Dim currLbl As Label=DirectCast(dli.FindControl(“label2″),Label)
    '…
Next


C#
foreach(DataListItem dli in DataList1.Items)
{
Label currLbl = (Label) dli.FindControl(“label2″);
//…

}

Tuesday, October 18, 2011

Postback Event when using SharePoint:PeopleEditor

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 get the data from a Database. The problem is, how to make the PeopleEditor send a signal to my code that a value has been set?! There is no event on the PeopleEditor control fires when a value is set.

few minuites of googling lead me to msdn   this article  ,
When you set the property AutoPostBack to true,  a Postback will be generated when a value is set in the control.

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, and the page is reloaded. There you can have a code in Page_Load that checks if a value has been selected and take some action like querying that database with the PeopleEditor user as a key and then filling other controls on the page.

string accountName = null;
if (peUser.ResolvedEntities.Count > 0)
 {
      PickerEntity entity = (PickerEntity)peUser.ResolvedEntities[0];
accountName = entity.Key;
      int pos = accountName.IndexOf('\\');
      accountName = accountName.Substring(pos + 1);

// take some action based on accountName


 }