1-888-365-2779
+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Adding Programmatic Dropdown User Field

Adding Programmatic Dropdown User Field

5 posts, 2 answered
  1. David
    David avatar
    50 posts
    Registered:
    14 Jan 2009
    31 Aug 2009
    Link to this post
    Hello,

    I'm using version 3.6
    I would like to add a dropdown list in the Add User / Edit User templates of Users.aspx. Essentially, there will be users and sub-users. An example of this would be a user for each company, and then sub-users which are employees of that company. To separate employees from companies, I have created two separate roles, "Employees" and "Companies". I need this dropdown to contain the list of users under the role "Companies". The list of companies will change dynamically, so I need to populate the list programmatically on page load.

    I have all of the elements working, except for populating the dropdown list. The insert & edit user templates are buried inside of users.ascx, so what is the best method for manipulating the dropdown control? I've attempted the following code, but it doesn't work. I think manageUsers.GetCurrentContainer() isn't returning a value.
    Control container = manageUsers.GetCurrentContainer(); 
             
            if (container is ManageUsers.EditContainer) 
            { 
                //Find the DropDownList control 
                DropDownList ddlist = container.FindControl("ddCompany"as DropDownList; 
                //Returns an array of usernames 
                string[] companies= GetCompanies(); 
                for (int i = 0; i < companies.Length; i++) 
                { 
                    ddlist.Items.Add(companies[i]); 
                } 
                 
            } 

    Am I on the right track?

    Thank you for your support,

    David
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    16 Jun 2017
    31 Aug 2009
    Link to this post
    Hello David,

    Below is a sample code that illustrates how to bind users from a certain role.

      void DropDownBind() 
        { 
            Control container = manageUsers.GetCurrentContainer(); 
            if (container is ManageUsers.EditContainer) 
            { 
                ManageUsers.EditContainer editCont = container as ManageUsers.EditContainer; 
                ManageProfile.EditContainer editProfile = editCont.ProfileControl.Controls[0] as ManageProfile.EditContainer; 
                
                DropDownList combo = editProfile.GetControl<DropDownList>("MyDropDownList"false); 
                string[] AllUsers = Roles.GetUsersInRole("administrators");; 
                combo.Items.Clear(); 
                foreach (string roleUser in AllUsers) 
                { 
                    ListItem item = new ListItem(roleUser, roleUser); 
                    combo.Items.Add(item); 
                } 
            } 
     
        } 


    Sincerely yours,
    Ivan Dimitrov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
    Answered
  3. David
    David avatar
    50 posts
    Registered:
    14 Jan 2009
    31 Aug 2009
    Link to this post
    Thank you very much for your timely response.

    Your solution is working well to set the value, but it doesn't pull the value when the user loads the page. Is there additional code to accomplish this, or should your solution do this as well?

    Thank you,

    David
  4. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    16 Jun 2017
    01 Sep 2009
    Link to this post
    Hi David,

    You should add the following logic OnPreRender method.

          ManageUsers.EditContainer editCont1 = manageUsers.Controls[0] as ManageUsers.EditContainer; 
            if (editCont1 != null) 
            { 
                ManageProfile.EditContainer editProfile1 = editCont1.ProfileControl.Controls[0] as ManageProfile.EditContainer; 
                if (editProfile1 != null) 
                { 
     
                    DropDownList combo = editProfile1.GetControl<DropDownList>("MyDropDownList", false); 
                    string username1 = manageUsers.UserName; 
                    ProfileBase profile = ProfileBase.Create(username1); 
                    string userValue = (string)profile.GetPropertyValue("MyDropDownList"); 
                    combo.SelectedValue = userValue; 
                } 
            } 


    All the best,
    Ivan Dimitrov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
    Answered
  5. David
    David avatar
    50 posts
    Registered:
    14 Jan 2009
    01 Sep 2009
    Link to this post
    As always, you're spot on!

    Thanks again,

    David
Register for webinar
5 posts, 2 answered