More in this section

Forums / Developing with Sitefinity / Enable textbox in custom module depending on user's role

Enable textbox in custom module depending on user's role

4 posts, 0 answered
  1. Nadya
    Nadya avatar
    52 posts
    Registered:
    17 Dec 2008
    25 Sep 2009
    Link to this post
    Hi, I am developing a website using Sitefinity 3.7 and I have a very simple Translation module. It only has 2 meta fields - name and value. I need if the user that wants to edit a translation item is in "Administrators" role the name textbox to be enabled. If the user is not in this role the name textbox will be disabled and only the value will be editable.
     I tried doing it by adding the following to TranslationEditView.ascx:

    ...... 
    <%@ Import Namespace="System.Web.Security" %> 
    ...... 
     
    <fieldset class="set"
                <div class="setIn title"
                    <asp:TextBox ID="Name" Text="<%$Resources:NameInput %>" runat="server" Enabled='<%= System.Web.Security.Roles.IsUserInRole("Administrators"); %>'></asp:TextBox> 
                    <asp:RequiredFieldValidator ID="validator1" runat="server" ControlToValidate="Name" 
                        Display="Dynamic" EnableViewState="False" CssClass="validMessage" SetFocusOnError="True"
                        <strong> 
                            <asp:Literal ID="Literal6" runat="server" Text="<%$Resources:NameEmpty %>"></asp:Literal></strong></asp:RequiredFieldValidator> 
                </div> 
            </fieldset> 


    But then I am getting an "

    Invalid member name "System".  exception, which I suppose is related to the fact that this file is a resource.


    Can you help me - I need it ASAP ?
    Thanks a lot!

    Best Greetings, 
    Nadya
    "
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    25 Sep 2009
    Link to this post
    Hello Nadya,

    It is not possible to use the server side code in the way you want because you are trying create an object of boolean tyoe from its string representation. Also executing server code in the template is not supported from Sitefinity parser. You should use ASP.NET parser. In your code you are importing System.Web.Security then again you are calling System.Web.Security.Roles.IsUserInRole("Administrators"); ( remove the semi-colon).

    I suggest that you should createa a boolean property in the code behind ( or inline) and then use the following syntaxis Enabled='<%# View %>' ( this will work if you bind the page) or call IsUserInRole inside Page_Load.

    I hope this helps.

    Greetings,
    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.
  3. Nadya
    Nadya avatar
    52 posts
    Registered:
    17 Dec 2008
    25 Sep 2009
    Link to this post
    Hi Ivan,

    I've tried these but none seems to work.
    I tried also adding a Inherits attribute :

    <%@ Control Language="C#" Inherits="TranslationEditViewHelper"%> 
     
     

    And using the simple class:

    using System.Web.Security; 
     
    namespace Translation.WebControls.Admin.Translation 
        public class TranslationEditViewHelper : System.Web.UI.UserControl 
        { 
            public string View 
            { 
                get 
                { 
                    return Roles.IsUserInRole("Administrators") ? "True" : "False"
                } 
            } 
        } 

    and trying to set the textbox using :

    Enabled='<%# View %>' 

    This is not working again.

    Is making this textbox enabled depending on roles possible considering that  TranslationEditView.ascx is embedded resource ?

    Thanks for quick reply,
    All Best,
    Nadya
  4. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    25 Sep 2009
    Link to this post
    Hello Nadya,

    You are crating a a property of string type instead of boolean.

     public bool View 
        { 
            get 
            { 
                if (System.Web.Security.Roles.IsUserInRole("administrators")) 
                    return true
                else 
                    return false
            } 
        } 

    Then you need to call Page.DataBind();

    You can also execute the code on Page_Load without setting the Enabled property declaratively

     if (System.Web.Security.Roles.IsUserInRole("administrato")) 
            { 
                TextBox1.Enabled = true
            } 
            else 
            { 
                TextBox1.Enabled = false
            } 

    Regards,
    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.
Register for webinar
4 posts, 0 answered