Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > What is syntax for Eval(User.IsInRole) to return True/False To Vis

Reply
Thread Tools

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

 
 
Morris Neuman
Guest
Posts: n/a
 
      12-29-2006
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
 
Reply With Quote
 
 
 
 
Steven Cheng[MSFT]
Guest
Posts: n/a
 
      12-29-2006
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.



 
Reply With Quote
 
 
 
 
Steven Cheng[MSFT]
Guest
Posts: n/a
 
      01-03-2007
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/subscripti...ult.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/subscripti...t/default.aspx.

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



This posting is provided "AS IS" with no warranties, and confers no rights.

 
Reply With Quote
 
MikeS
Guest
Posts: n/a
 
      01-03-2007
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>

 
Reply With Quote
 
MikeS
Guest
Posts: n/a
 
      01-03-2007
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.

 
Reply With Quote
 
Steven Cheng[MSFT]
Guest
Posts: n/a
 
      01-04-2007
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.






 
Reply With Quote
 
MikeS
Guest
Posts: n/a
 
      01-05-2007
> 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.

 
Reply With Quote
 
Steven Cheng[MSFT]
Guest
Posts: n/a
 
      01-05-2007
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.



 
Reply With Quote
 
Morris Neuman
Guest
Posts: n/a
 
      01-09-2007
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

 
Reply With Quote
 
Steven Cheng[MSFT]
Guest
Posts: n/a
 
      01-09-2007
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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with XHTML validation in visual studio 2005 on windows vis =?Utf-8?B?SmVhbi1DaHJpc3RpYW4=?= ASP .Net 0 12-14-2006 09:31 PM
compile .NET 1.1 apps with Vis Studio 2005? K. Abit ASP .Net 1 11-25-2006 07:27 PM
Tkinter from Vis. BASIC Terrance N. Phillip Python 2 11-19-2005 06:54 PM
FWD/Pulver vis Skype SidKnee VOIP 3 03-12-2005 07:47 AM
DVI to HDMI converters and Vis versa any difference ? Helllo DVD Video 0 02-02-2005 01:31 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57