What is syntax for Eval(User.IsInRole) to return True/False To Vis

Discussion in 'ASP .Net Web Controls' started by Morris Neuman, Dec 29, 2006.

  1. I am trying to set the Visible property attribute of a asp:HyperLink control
    based on the user's membership role. If the user role is administrator then
    I want to display the hyperlink, if not then I want the hyperlink to be
    invisible.

    I tried setting:
    Visible="<%# Eval(Convert.ToString(User.IsInRole ("administrator")))%>"

    This gives me server tag is not well formed error. I have tried variations
    for the syntax but have not had any success.

    Any help/guidance would be appreciated.
    --
    Thanks
    Morris
     
    Morris Neuman, Dec 29, 2006
    #1
    1. Advertising

  2. Hello Morris,

    As for the databinding expression in your scenario, you do not need to use
    "Eval" function since it is used for retrieved data field value from
    datasource item only. You can simply put the boolean expression in
    databinding block directly as below:

    ======================
    <asp:Button ID="btn1" runat="server" Text="Show TextBox1"
    .............
    Enabled='<%# User.IsInRole("administrator") %>'/>

    ======================

    Hope this helps.

    Sincerely,

    Steven Cheng

    Microsoft MSDN Online Support Lead


    This posting is provided "AS IS" with no warranties, and confers no rights.
     
    Steven Cheng[MSFT], Dec 29, 2006
    #2
    1. Advertising

  3. RE: What is syntax for Eval(User.IsInRole) to return True/False To

    Hello Morris,

    Thanks for your reply.

    I think the problem now is not due to syntax error. The Visible='<%#
    User.IsInRole("administrator") %>'/> like expression should correctly
    return a boolean value. Based on my analysis, what you can check are the
    following two things:

    1. programmatically print out the User.IsInRole("administrator") value to
    make sure it return the expected value (use response.write....).

    2. for <%# User.IsInRole("administrator") %> expression, it is used for
    databinding(different from <% %> expression), and it won't be executed
    automatically if you do not perform databinding on the expression's
    container or super container. Therefore, you should make sure you have
    called databind method on the control which contains this expression. e.g.

    ================
    <asp:LinkButton ID="LinkButton1" runat="server"
    Visible='<%# User.IsInRole("admin")
    %>'>LinkButton</asp:LinkButton><br />


    protected void Page_Load(object sender, EventArgs e)
    {

    LinkButton1.DataBind();

    }
    ====================

    Hope this helps.

    Sincerely,

    Steven Cheng

    Microsoft MSDN Online Support Lead



    ==================================================

    Get notification to my posts through email? Please refer to
    http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
    ications.



    Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
    where an initial response from the community or a Microsoft Support
    Engineer within 1 business day is acceptable. Please note that each follow
    up response may take approximately 2 business days as the support
    professional working with you may need further investigation to reach the
    most efficient resolution. The offering is not appropriate for situations
    that require urgent, real-time or phone-based interactions or complex
    project analysis and dump analysis issues. Issues of this nature are best
    handled working with a dedicated Microsoft Support Engineer by contacting
    Microsoft Customer Support Services (CSS) at
    http://msdn.microsoft.com/subscriptions/support/default.aspx.

    ==================================================



    This posting is provided "AS IS" with no warranties, and confers no rights.
     
    Steven Cheng[MSFT], Jan 3, 2007
    #3
  4. Morris Neuman

    MikeS Guest

    Perhaps a loginview control will work for you.

    <asp:LoginView ID="LoginView1" runat="server">
    <RoleGroups>
    <asp:RoleGroup Roles="Administrator">
    <ContentTemplate>
    <asp:HyperLink ID="HyperLink1"
    runat="server">HyperLink</asp:HyperLink>
    </ContentTemplate>
    </asp:RoleGroup>
    </RoleGroups>
    </asp:LoginView>
     
    MikeS, Jan 3, 2007
    #4
  5. Morris Neuman

    MikeS Guest

    Re: What is syntax for Eval(User.IsInRole) to return True/False To

    You might nest a second loginview control, that contains only the
    hyperlink for the specific rolegroup, inside the first loginview
    control.

    To me that seems more consistent with the toolsets features than
    calling IsUserInRole to manipulate the UI.
     
    MikeS, Jan 3, 2007
    #5
  6. RE: What is syntax for Eval(User.IsInRole) to return True/False To

    Thanks for your reply Morris,

    Sure, this is very important and would cause the original code in the last
    reply not work. When you put the control in Master Page, they has no direct
    reference member in concrete pages(applied that master page), you need to
    use FindControl to locate the certain control if you want to access it.

    Here, you have the following options:

    1. Just put the databinding code in MasterPage's Load event. e.g.

    ========
    public partial class masters_simple : System.Web.UI.MasterPage
    {
    protected void Page_Load(object sender, EventArgs e)
    {
    HyperLink1.DataBind();
    }
    }
    ==============
    However, you need to adjust the master template also since "User" property
    is not directly accessible in MasterPage class. e.g.

    ======in master template===========
    <asp:HyperLink ID="HyperLink1" runat="server"
    NavigateUrl="http://www.asp.net"
    Visible='<%# Page.User.IsInRole("admin") %>'
    >Test Hyperlink</asp:HyperLink>

    ===============

    2. If you want to put the databinding in concrete page code, you need to
    first programmatically reference the control instance and call "DataBind"
    method on it. e.g.

    =====================
    public partial class Part2_contentPage : System.Web.UI.Page
    {
    protected void Page_Load(object sender, EventArgs e)
    {
    Master.FindControl("HyperLink1").DataBind();
    }
    }
    =====================

    Hope this helps.

    Sincerely,

    Steven Cheng

    Microsoft MSDN Online Support Lead


    This posting is provided "AS IS" with no warranties, and confers no rights.
     
    Steven Cheng[MSFT], Jan 4, 2007
    #6
  7. Morris Neuman

    MikeS Guest

    Re: What is syntax for Eval(User.IsInRole) to return True/False To

    > Would the second nested login view require duplicate ui controls?

    It shouldn't since the second loginview control would only hold the
    hyperlink in the rolegroup template, and hold nothing else.
     
    MikeS, Jan 5, 2007
    #7
  8. RE: What is syntax for Eval(User.IsInRole) to return True/False To

    Hi Morris,

    Thanks for your followup.

    Sure, if your Hyperlink control is nested in other container control(not
    directly on the master page's top level), you can not directly find it
    through MasterPage.Findcontrol... The code in my last reply just indicate
    that this is a possible approach but won't 100% since your actual master
    page control layout is unknown to me.

    I suggest you provide your master page's layout info(at least how your
    hyperlink reside on the page and its container). Here I just use a test
    master page which use a LoginView to hold the hyperlink and access it from
    concrete page. Here is the code

    ==========master page aspx=============
    ........................
    <asp:LoginView ID="LoginView1" runat="server">
    <AnonymousTemplate>
    <asp:HyperLink ID="HyperLink2" runat="server"
    Visible='<%# Page.User.IsInRole("admin") %>'
    >HyperLink in LoginView</asp:HyperLink>

    </AnonymousTemplate>
    </asp:LoginView>
    ..............
    ============content page codebehind==========
    protected void Page_Load(object sender, EventArgs e)
    {

    LoginView lv = Master.FindControl("LoginView1") as LoginView;

    HyperLink link = lv.FindControl("HyperLink2") as HyperLink;

    if(link !=null)
    {
    link.DataBind();
    }

    }
    ====================================

    Please let me know if you have anything unclear or any further questions on
    this.

    Sincerely,

    Steven Cheng

    Microsoft MSDN Online Support Lead


    This posting is provided "AS IS" with no warranties, and confers no rights.
     
    Steven Cheng[MSFT], Jan 5, 2007
    #8
  9. RE: What is syntax for Eval(User.IsInRole) to return True/False To

    Hi Steven,

    I was not ble to see your posting thru Outlook Express.

    As suggested, I have emailed you test pages via Outlook. Email subject is
    "Repro page for Re:What is syntax for Eval(user.IsInRole) to return ...."

    As always, your help and feedback are much appreciated.
    --
    Thanks
    Morris


    "Steven Cheng[MSFT]" wrote:

    > Thanks for your reply Morris,
    >
    > So far I haven't found any particular thing incorrect here. Would you
    > create a simple repro page(1 master page + 1 content page) and send it to
    > me (through the email in my signature and remove "online")? Thus, I can
    > test it in my local environment. Also, I've attached my test pages in this
    > message for your reference. You can get them by visiting the thread through
    > Outlook Express.
    >
    > Please feel free to let me know if there is anything unclear.
    >
    > Sincerely,
    >
    > Steven Cheng
    >
    > Microsoft MSDN Online Support Lead
    >
    >
    > This posting is provided "AS IS" with no warranties, and confers no rights
     
    Morris Neuman, Jan 9, 2007
    #9
  10. RE: What is syntax for Eval(User.IsInRole) to return True/False To

    Hi Morris,

    I've just checked your code and found out what's the problem. Actually, it
    is a simple mistake. I originally thought that the "HyperLink1"(which need
    to be hide) is on the Master Page. However, from your page code, it is on
    content page's template. Thus, you do not need to use Master.FindControl
    to reference the LoginView, you can directly reference the LoginView
    control and call FindControl on it to locate the Hyperlink. Below is the
    modified code of "default.aspx", I've tested and it worked correctly in my
    local environment(for your test project).

    ========================
    <script runat="server">

    protected void Page_Load(object sender, EventArgs e)
    {


    Response.Write("<br/>IsUserInRole('administrator'): " +
    User.IsInRole("administrator"));

    // You do not need to use Master.FindControl here.
    //directly call Findcontrol on the LoginView member
    LoginView lv = this.LoginView1;

    if (lv != null)
    {
    HyperLink myControl1 = lv.FindControl("HyperLink1") as
    HyperLink;

    if (myControl1 != null)
    {
    Response.Write(myControl1.ToString());
    myControl1.DataBind();
    }
    }
    }
    </script>
    =============================

    Hope this helps.

    Sincerely,

    Steven Cheng

    Microsoft MSDN Online Support Lead


    This posting is provided "AS IS" with no warranties, and confers no rights.
     
    Steven Cheng[MSFT], Jan 9, 2007
    #10
  11. RE: What is syntax for Eval(User.IsInRole) to return True/False To

    Cheers :)
     
    Steven Cheng[MSFT], Jan 10, 2007
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Siemel Naran

    Does true ^ true return false?

    Siemel Naran, Jun 17, 2004, in forum: C++
    Replies:
    19
    Views:
    669
    Chris Theis
    Jun 18, 2004
  2. André
    Replies:
    3
    Views:
    1,596
  3. Replies:
    0
    Views:
    835
  4. bdb112
    Replies:
    45
    Views:
    1,350
    jazbees
    Apr 29, 2009
  5. w i l l
    Replies:
    4
    Views:
    243
    Dan Brussee
    Jul 4, 2003
Loading...

Share This Page