SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm in the process of working thorugh Microsofts video tutorials for developing on Sharepoint. My actual small project is to create a visual Webpart which displays a list of all registered users with their display name, department and title.

This works like a charm. Now I want to improve the design of this little piece, just for the sake of it. So I created an . How can I put my rendered controls right into this Panel?

Below is the code which is executed when the Page loads.

public partial class PeoplePickerUserControl : UserControl
    protected void Page_Load(object sender, EventArgs e)
        string _department = null, _Title = null;
        var serviceContext = SPServiceContext.GetContext(SPContext.Current.Site);
        var userProfileManager = new UserProfileManager(serviceContext);

        Controls.Add(new LiteralControl("<div class='ui-widget-header'>PeoplePicker<div class='ui-widget-content ui-corner-all'>"));
        foreach (UserProfile userProfile in userProfileManager)
            if (userProfile[PropertyConstants.Department].Value != null)
                _department = userProfile[PropertyConstants.Department].Value.ToString();
            else _department = "";
            if (userProfile[PropertyConstants.Title].Value != null)
                _Title = userProfile[PropertyConstants.Title].Value.ToString() + ", ";
            else _Title = "";

            Controls.Add(new LiteralControl("<p><fieldset>"+ " " +"<a href='" + userProfile.PublicUrl + "'>"+ userProfile.DisplayName + "</a><br />" 
                + _Title +  _department + "</fieldset></p>"));
        Controls.Add(new LiteralControl("</div></div>"));

<asp:Panel ID="staffContentInHerePanel" runat="server">
   // Content should be put in here

As you can see I am rendering two divs, which I want to be styled with JQuery. This part shall be exported into the .ascx file.

Thank you in advance.

share|improve this question
up vote 3 down vote accepted

Try to use staffContentInHerePanel.Controls instead of just Controls in your code

Something like this...

staffContentInHerePanel.Controls.Add(new LiteralControl("</div></div>"));
share|improve this answer
Simple as that, Works like charm. I'd like to vote you up, but I'm lacking reputation. best regards. – Marco Jul 18 '12 at 8:35

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.