Home All Groups Group Topic Archive Search About

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

Author
29 Dec 2006 12:48 AM
Morris Neuman
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

Author
29 Dec 2006 7:34 AM
Steven Cheng[MSFT]
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.
Author
29 Dec 2006 1:53 PM
Morris Neuman
Hi Steven,

Thanks for the quick response, however it did not work.

Per your instructions, tried both a button and hyperlink control, to be
enabled only if the user is assigned administrator role.  However, the button
was enabled even when I logged in using a non-administrator userid.

I have a hyperlink control on a form that I only want visible if the user
logging in is an administrator.

I tried the following scenario:

<asp:HyperLink ID="HyperLink1" runat="server"
NavigateURL="~/AdministratorOnly/Registration.aspx" Visible='<%#
User.IsInRole("administrator")%>' >Administer Website</asp:HyperLink>

I also tried the above with Enabled instead of Visible.

In both cases (enabled and visible), I don't get a syntax error but the
HyperLink shows and is active even when I log in with a non administrator
user id.  Because the page in the navigateurl is only accessible to
administrator role, when I click on it I can't see it. 

However I don't want to display the link when a non administrator logs in. 
How can I do that.

Once again any help is much appreciated.
--
Thanks
Morris


Show quoteHide quote
"Steven Cheng[MSFT]" wrote:

> 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.
>
>
>
>
Author
3 Jan 2007 1:14 AM
Steven Cheng[MSFT]
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.
Author
3 Jan 2007 2:28 PM
Morris Neuman
Hi,

I added the data bind as suggested for the HyperLink1 control and get error
that page does not contain a definition for 'HyperLink1'.

My page uses a master page so could that be the problem?

My code includes the following:

===============
<%@ Page Language="C#" MasterPageFile="~/MasterPage1.master" Title="Home
Page" %>

<script runat="server">

   protected void Page_Load(object sender, EventArgs e)
   {
          HyperLink1.DataBind();
   }

</script

<LoggedInTemplate>

   <asp:HyperLink ID="HyperLink1" runat="server"
NavigateURL="~/AdminOnly/Registration.aspx" Visible='<%#
User.IsInRole("administrator") %>' >Administer Website</asp:HyperLink>

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

Any help is most appreciated.
--
Thanks
Morris


Show quoteHide quote
"Steven Cheng[MSFT]" wrote:

> 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.
>
>
Author
4 Jan 2007 1:19 AM
Steven Cheng[MSFT]
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.
Author
4 Jan 2007 6:58 PM
Morris Neuman
Steven

I tried method 2

protected void Page_Load(object sender, EventArgs e)
    {
        Master.FindControl("HyperLink1").DataBind();
    }

But it does not find the control.  I put a break point in the code and saw
that .FindControl(("HyperLink1") returns "null". 

The hyperlink control is on LoggedInTemplate of a LoginView.  Do I need to
do something else to reference that control on a LoggedInTemplate?

--
Thanks
Morris


Show quoteHide quote
"Steven Cheng[MSFT]" wrote:

> 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.
>
>
>
>
>
>
>
Author
5 Jan 2007 10:22 AM
Steven Cheng[MSFT]
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.
Author
5 Jan 2007 3:46 PM
Morris Neuman
Hi Steven,
Sorry if I have not been providing all the necessary information and thank
you for your patience.

My page layout is as follows:
I have a content page that has a LoginView control.  The content page also
uses a Master Page.

The LoginView has an Annonymous template and a LoggedIn template. 

The LoggedIn template displays data in a GridView based on the login
username and also displays a hyperlink control "HyperLink1".  This link is to
a page that lets you administer the website via WAT, so I only want
administerators to see the link.

I need HyperLink1 to only be displayed if the user that loggedin is in role
"administrator".

I tried what you suggested but the hyperlink is still visible even if I log
in with ID not in administrator role.

In fact the variable lv (for LoginView control) is always null for every
postback.  I set a break point and checked using the debugger.

How can I reference LoginView Template controls on a page that uses a Master
Page?  It seems that the FindControl does not work in this scenario.

Maybe Page_load() is not the right point to do the FindControl() on the
LoginView?  Is there a later stage event for the page where we are sure the
content page, the master page and the template to be used for the LoginView
are merged and resolved so the FindControl will work?

I have code as follows:

==========master page aspx=============
.........................
<asp:LoginView ID="LoginView1" runat="server">
                        <LoggedInTemplate>
                            <asp:HyperLink ID="HyperLink1" runat="server"
                            Visible='<%# Page.User.IsInRole("administrator")
%>'
>Administer Website</asp:HyperLink>
                        </LoggedInTemplate>
                    </asp:LoginView>
...............
============content page codebehind==========
  protected void Page_Load(object sender, EventArgs e)
    {

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

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

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

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


--
Thanks
Morris


Show quoteHide quote
"Steven Cheng[MSFT]" wrote:

> 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.
>
>
>
>
Author
8 Jan 2007 8:28 AM
Steven Cheng[MSFT]
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.
Author
9 Jan 2007 3:15 AM
Morris Neuman
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


Show quoteHide quote
"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
Author
9 Jan 2007 7:33 AM
Steven Cheng[MSFT]
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.
Author
9 Jan 2007 2:05 PM
Morris Neuman
It worked!!!!

Thanks so very much for all your help.  We are now on the way to deploying
our first .net web application.
--
Thanks again
Morris


Show quoteHide quote
"Steven Cheng[MSFT]" wrote:

> 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.
>
>
Author
10 Jan 2007 12:54 AM
Steven Cheng[MSFT]
Cheers :)
Author
3 Jan 2007 3:09 PM
MikeS
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>
Author
3 Jan 2007 4:59 PM
Morris Neuman
Thanks Mike.

I know that creating a RolesGroup template in addition to the LoggedIn
Template in the LoginView would work.  However I am trying not to duplicate
all the other controls that would be same between the 2 templates.  It is
just this one hyperlink that I need to display based on the user role.  I was
hoping to do that with the visible attribute of the control.
--
Thanks
Morris


Show quoteHide quote
"MikeS" wrote:

> 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>
>
>
Author
3 Jan 2007 9:12 PM
MikeS
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.
Author
3 Jan 2007 10:22 PM
Morris Neuman
Thanks again Mike.

Would the second nested login view require duplicate ui controls?

I am learning this product and appreciate the input and help.
--
Thanks
Morris


Show quoteHide quote
"MikeS" wrote:

> 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.
>
>
Author
5 Jan 2007 3:33 AM
MikeS
> 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.