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

M

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.
 
S

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.
 
S

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.
 
M

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>
 
M

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.
 
S

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=' said:
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.
 
M

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.
 
S

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=' said:
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.
 
M

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.
 
S

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top