More in this section

Forums / Developing with Sitefinity / Role picker WebEditor

Role picker WebEditor

7 posts, 0 answered
  1. Andy
    Andy avatar
    62 posts
    Registered:
    01 Aug 2008
    22 Jun 2009
    Link to this post

    (in v3.6) I'm trying to write a webeditor to select a Role.
    I'm not sure I'm going about this the right way, but I'm trying to write a WebEditor similar to UrlWebEditor, but containing a ManageUsers control rather than a ManageFiles control, and setting manageUsers.Mode = ManageUsers.Modes.Roles.

     class RolePicker : WebUITypeEditor<Guid> 
        { 
            // Fields 
            private ManageUsers manageUsers; 
     
            // Methods 
            protected override void CreateChildControls() 
            { 
                this.Controls.Clear(); 
                this.manageUsers = new ManageUsers(); 
                this.manageUsers.Mode = ManageUsers.Modes.Roles; 
                this.manageUsers.UserName = "andy";            
                this.Controls.Add(this.manageUsers); 
            } 
     
            ...etc 
     
        } 

    Trouble is, I think (?) that manageUsers in the Mode requires a template (ascx) to be set up, and I have no idea what the default template for this should look like.

    Can you give me a pointer in the right direction?

    Thanks!
    Andy

  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    22 Jun 2009
    Link to this post
    Hi Andy,

    You can create a property that can be used for setting the Role template.

    Sample code:

    public string TemplatePath 
        { 
            get 
            { 
                object o = this.ViewState["templatePath"]; 
                if (o == null
                    return "~/CustomControls/Roles.ascx"
                return (string)o; 
            } 
            set 
            { 
                this.ViewState["templatePath"] = value; 
            } 
        } 
     

    I suggest that you should use WebUITypeEditor<string>,  because you cannot get the ID of a role.
    In your template you should add control that will show the result - repeater, RadGrid  etc.

    Sincerely yours,
    Ivan Dimitrov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  3. Andy
    Andy avatar
    62 posts
    Registered:
    01 Aug 2008
    23 Jun 2009
    Link to this post
    Thanks for the reply Ivan,

    Might be a problem that the Role Guid isn't returned, but in any case what I really meant was that my code was giving me an error:

    A required control was not found in the template for "". The control must be assignable form type "System.Web.UI.WebControls.ListControl" and must have ID "RolesList".

    So I assumed that I needed to point it at an ascx, that the asc needed a ListControl named RolesList in it... but I wasn't sure what else it should have, or if there was a default template for this hidden somewhere that I could use and maybe extend if I needed to.

    With regard to actually setting the template, can manageUsers.RolesTemplate or .RolesTemplatePath be used for this, or should I use your code?

    Kind regards, Andy
  4. Andy
    Andy avatar
    62 posts
    Registered:
    01 Aug 2008
    23 Jun 2009
    Link to this post
    Oh, and the picker is living in a custrom module, so the Roles.ascx should come from the Resource. How do I connect that up?
  5. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    23 Jun 2009
    Link to this post
    Hello Andy,

    Your approach will not work. Also the error is caused by missing control in your template. The ID of a role cannot be returned as I told you in the previous post. You can get the role name

    Please find below the Role Name selector prepared for your convenience.

    Create a custom class in App_Code

    using System; 
    using System.Collections.Generic; 
    using System.Linq; 
    using System.Web; 
    using Telerik.Cms.Web.UI; 
    using System.Web.UI; 
    using Telerik.Framework.Web; 
    using System.Web.UI.WebControls; 
    using System.Web.Security; 
     
    /// <summary> 
    /// Summary description for CustomRoleSelector 
    /// </summary> 
    public class CustomRoleSelector : WebUITypeEditor<string>  
        {  
            public CustomRoleSelector()  
            {  
            }  
            #region Properties  
            public override string Value  
            {  
                get  
                {  
                    string o = (string)this.ViewState["Name"];  
                    if (o == null)  
                        return string.Empty;  
                    else  
                    {  
      
                        return (string)o;  
                    }  
                }  
                set  
                {  
                    this.ViewState["Name"] = value;  
                }  
            }  
      
            public string TemplatePath  
            {  
                get  
                {  
                    object o = this.ViewState["templatePath"];  
                    if (o == null)  
                        return "~/CustomControls/CustomTemplate.ascx";  
                    return (string)o;  
                }  
                set  
                {  
                    this.ViewState["templatePath"] = value;  
                }  
            }  
     
            #endregion  
     
            protected virtual GridView ItemsGrid  
            {  
                get { return this.Controls[0].FindControl("itemsGrid"as GridView; }  
            }  
              
            protected override void CreateChildControls()  
            {  
                this.template = ControlUtils.GetTemplate<DefaultSelectorTemplate>(this.TemplatePath);  
                this.template.InstantiateIn(this);  
      
                base.CreateChildControls();  
               string[] allroles = Roles.GetAllRoles(); 
      
                if (allroles.Length != 0)  
                {  
                    this.ItemsGrid.RowDataBound += new GridViewRowEventHandler(ItemsGrid_RowDataBound);  
                    this.ItemsGrid.RowCommand += new GridViewCommandEventHandler(ItemsGrid_RowCommand);                
                    this.ItemsGrid.DataSource = allroles;  
                    this.ItemsGrid.DataBind();  
                }  
            }  
      
            void ItemsGrid_RowDataBound(object sender, GridViewRowEventArgs e)  
            {  
                if (e.Row.RowType == DataControlRowType.DataRow)  
                    if (e.Row.Cells.Count > 1)  
                    {  
     
                        Label lbl = e.Row.FindControl("label1"as Label;  
                        if (lbl != null)  
                        {  
                          lbl.Text = (string)e.Row.DataItem; 
                           
                        }  
                    }      
            }  
      
            void ItemsGrid_RowCommand(object sender, GridViewCommandEventArgs e)  
            {  
                switch (e.CommandName)  
                {  
                    case "Select"
                        GridView grid = e.CommandSource as GridView; 
                        GridViewRow row = grid.Rows[Convert.ToInt32(e.CommandArgument)]; 
     
                        if (row.Cells.Count > 1) 
                        { 
                            Label lbl = row.FindControl("label1"as Label; 
                            this.ViewState["Name"] = lbl.Text; 
                            OnValueChanged(new ValueChangedEventArgs(this.Value));  
                            
                        } 
                        break;  
                }  
            }  
     
            #region Private fields  
            private ITemplate template;  
            #endregion  
          
      
        public class DefaultSelectorTemplate : ITemplate  
        {  
            public void InstantiateIn(Control container)  
            {  
      
            }  
        } 

    Create a custom template for your selector:

    <asp:GridView runat="server" ID="itemsGrid" AutoGenerateColumns="false" PageSize="13" GridLines="none" CssClass="listItems">  
    <Columns>  
        <asp:CommandField SelectText="Select" ShowSelectButton="true" />  
        <asp:TemplateField>  
        <ItemTemplate>  
        <asp:Label ID="label1" runat="server"></asp:Label>  
          
        </ItemTemplate>  
        </asp:TemplateField>  
    </Columns>  
    </asp:GridView> 

    Add the selector to your control

        [WebEditor("CustomRoleSelector, App_Code")] 
        public string RoleName 
        { 
            get { return this.roleName; } 
            set { this.roleName = value; } 
        } 
        private string roleName; 



    Sincerely yours,
    Ivan Dimitrov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  6. Andy
    Andy avatar
    62 posts
    Registered:
    01 Aug 2008
    25 Jun 2009
    Link to this post
    Thanks for your detailed replay Ivan. However it didn't quite do things in the way I wanted - maybe I didn't make my question clear.

    Part of what I was asking was curiosity of how the ManageUsers control worked, and if I could use it for the innards of my custom dialogue. I was curious to how it looked and behaved in the various modes, and what the default template for each mode should look like, what controls were needed, etc. I was thinking it might be able to use it in other scenarios, not just for picking roles. In the end I had to go digging around with Reflector to find the controls that were needed, and actually that the Edit template was needed for Roles mode.

    The Role picker I'm trying to create is actually a context/user sensitive one, so the list of Roles available depends on who you are and why you are trying to select Roles for. This is all within a pluggable module, so I wanted the role picker to be specific to that, and for the template ascx's to be in the resource.

    In conclusion, I decided that the ManageUsers control wasn't going to help (as you know). I found out that I could load templates from the resource using something like:

    ITemplate rolesTemplate = ControlUtils.GetTemplate("""MyAssembly.Resources.ControlTemplates.Backend.WebEditors.RolePicker_Roles.ascx"this.GetType()); 

    Which was another of my questions that perhaps I wasn't clear on my motivation.

    I am accessing Roles via Telerik.DataAccess.AspnetProviders, and some custom Nolics dbclasses. So I can get SF Role objects with RoleId which is what I wanted. I'm just doing my own 'middle' for the picker now.

    Thanks for your help though. I think it would be really useful to get some better documentation for some of the basic API for SiteFinity. It would be really useful in addition to the basic info to give an idea of what the method is useful for, and an example of it's use in context, and if it is deprecated or not.

    Kind regards,
    Andy
  7. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    25 Jun 2009
    Link to this post
    Hello Andy,

    Let us know if you need further assistance. You can extend the code and create a new logic and templates for it.

    Greetings,
    Ivan Dimitrov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
Register for webinar
7 posts, 0 answered