This is a migrated thread and some comments may be shown as answers.

Child menu item link redirect based upon .net Role

6 Answers 126 Views
Menu
This is a migrated thread and some comments may be shown as answers.
stuart
Top achievements
Rank 1
stuart asked on 20 Jan 2009, 01:56 AM
Hello,

I am using Forms Authentication and ASP.NET roles for my website. For most of my menu links I will be able to use the NavigateUrl to open the proper pages. However, I need to be able to redirect users based upon their role for a few of the child items. For example, when an employee is in the parent branch named Employee and navigates to and clicks on the Forms child menu, I need all the users in the 'Doctors' role to be directed to ~/employee_login/doctors_area/forms.aspx and users in the 'Office_Staff' role to be directed to ~/employee_login/office_staff_area/forms.aspx page. I am able to accomplish this with the onclick event using a standard asp:LinkButton for example (see code below) but am unable to find a way of doing it using the RadMenu. Please help.

<

 

form id="form1" runat="server">

 

 

<div>

 

 

 

<asp:LinkButton ID="LinkButton1" runat="server" onclick="redirectDocStaff">LinkButton</asp:LinkButton><script runat="server"> protected void redirectDocStaff(object sender, EventArgs e)

 

{

 

if (Roles.IsUserInRole("Doctors"))

 

Response.Redirect(

"~/Employee_Login/Doctors_Area/forms.aspx");

 

 

else if (Roles.IsUserInRole("Office_Staff"))

 

Response.Redirect(

"~/Employee_Login/Office_Staff_Area/forms.aspx");

 

}

</

 

script>

 

6 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 20 Jan 2009, 07:47 AM
Hello Stuart,

You can try out the following code to set the NavigateUrl of childItems in RadMenu based on the user role:
aspx:
 <telerik:RadMenu ID="RadMenu1" Flow="Vertical" runat="server" OnPreRender="RadMenu1_PreRender" > 
       <Items> 
       <telerik:RadMenuItem  Text="Employees"
       <Items> 
       <telerik:RadMenuItem Text="Forms">       
       </telerik:RadMenuItem>       
       </Items> 
       </telerik:RadMenuItem>       
       </Items>         
 </telerik:RadMenu> 

cs:
 protected void RadMenu1_PreRender(object sender, EventArgs e) 
    { 
        if(Roles.IsUserInRole("Doctors")) 
        { 
            foreach (RadMenuItem rootItem in RadMenu1.Items) 
            { 
                if (rootItem.Text == "Employees") 
                { 
                    foreach (RadMenuItem childItem in rootItem.Items) 
                    { 
                        childItem.NavigateUrl = "employee_login/doctors_area/forms.aspx"
                    } 
                } 
            } 
        } 
    } 

Thanks
Princy.
0
stuart
Top achievements
Rank 1
answered on 20 Jan 2009, 06:11 PM
Hello Princy,

Thanks for the code. It worked for when a doctor signed in but when an Office_staff user signed in it did nothing. I am assuming I need to add ELSE... but am having no luck with it working. Can you show me the way it should look?

thanks.

Stuart
0
stuart
Top achievements
Rank 1
answered on 20 Jan 2009, 07:03 PM
Please disregard my last question as I did not realize I was still supposed to populate the navigation URL with the Office_Staff URL. but it does raise another two questions:

1. how should the code look if I would like to have more than two groups (i.e. 'doctors', 'office_staff', 'contractor', 'patient', etc)?
2. what if I wanted to implement your code solution into multiple child menus. see below for layout:
    Employee(parent) Policies(child) - the user is then redirected to one of four different pages depending on their Role.
    Employee(parent) Forms(child) -   the user is then redirected to one of four different pages depending on their Role.


0
Princy
Top achievements
Rank 2
answered on 21 Jan 2009, 07:04 AM
Hi Stuart,

You can try out the following code to achieve the required scenario.
cs:
 protected void RadMenu1_PreRender(object sender, EventArgs e) 
    {      
        if(Roles.IsUserInRole("Doctors"))  
        {  
            foreach (RadMenuItem rootItem in RadMenu1.Items)  
            {  
                if (rootItem.Text == "Employees")  
                {  
                    foreach (RadMenuItem childItem in rootItem.Items)  
                    { 
                        if(childItem.Text=="Forms")                         
                          childItem.NavigateUrl = "Default.aspx";  
 
                        if(childItem.Text=="Policies") 
                            childItem.NavigateUrl = "Default2.aspx";     
                        // similarly you can set the navigate url for other child items                      
                    }  
                }  
            }  
        } 
        else if (Roles.IsUserInRole("Patient")) 
        { 
            foreach (RadMenuItem rootItem in RadMenu1.Items) 
            { 
                if (rootItem.Text == "Employees") 
                { 
                    foreach (RadMenuItem childItem in rootItem.Items) 
                    { 
                        if (childItem.Text == "Forms") 
                            childItem.NavigateUrl = "Default3.aspx"
 
                        if (childItem.Text == "Policies") 
                            childItem.NavigateUrl = "Default4.aspx"
                    } 
                } 
            } 
        }    
        // similarly you can check for other existing roles 
    } 

Thanks
Princy.
0
stuart
Top achievements
Rank 1
answered on 23 Jan 2009, 06:30 PM
Hello Princy,

I have been working on the code for a couple of days and have been unable to get the child menus populate the url paths when logged in with the new code. I do not receive any errors when the page is rendered. below is the code for the page:

<%

@ Page Language="C#" AutoEventWireup="true" CodeFile="menutest.aspx.cs" Inherits="Secure_Employee_Login_Office_Staff_Area_menutest" %>

 

<%

@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>

 

<!

 

DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

<

 

script runat="server">

 

 

protected void RadMenu1_PreRender(object sender, EventArgs e)

 

{

 

if (Roles.IsUserInRole("Doctors"))

 

{

 

foreach (RadMenuItem rootItem in RadMenu1.Items)

 

{

 

if (rootItem.Text == "Employees")

 

{

 

foreach (RadMenuItem childItem in rootItem.Items)

 

{

 

if (childItem.Text == "Forms")

 

childItem.NavigateUrl =

"~/Secure/Employee_Login/Doctors_Area/Forms.aspx";

 

 

if (childItem.Text == "Policies")

 

childItem.NavigateUrl =

"~/Secure/Employee_Login/Doctors_Area/Policies.aspx";

 

 

// similarly you can set the navigate url for other child items

 

}

}

}

}

 

else if (Roles.IsUserInRole("Patient"))

 

{

 

foreach (RadMenuItem rootItem in RadMenu1.Items)

 

{

 

if (rootItem.Text == "Employees")

 

{

 

foreach (RadMenuItem childItem in rootItem.Items)

 

{

 

if (childItem.Text == "Forms")

 

childItem.NavigateUrl =

"~/Secure/Employee_Login/Office_Staff_Area/Forms.aspx";

 

 

if (childItem.Text == "Policies")

 

childItem.NavigateUrl =

"~/Secure/Employee_Login/Office_Staff_Area/Policies.aspx";

 

}

}

}

}

 

// similarly you can check for other existing roles

 

}

 

</script>

 

<

 

html xmlns="http://www.w3.org/1999/xhtml">

 

<

 

head runat="server">

 

 

<title></title>

 

</

 

head>

 

<

 

body>

 

 

<form id="form1" runat="server"> <asp:ScriptManager ID="ScriptManager1" runat="server" />

 

 

<div>

 

 

<telerik:RadMenu ID="RadMenu1" runat="server" OnPreRender="RadMenu1_PreRender" >

 

<

 

CollapseAnimation Type="OutQuint" Duration="200"></CollapseAnimation>

 

 

<Items>

 

 

<telerik:RadMenuItem runat="server" Text="Employees">

 

 

<Items>

 

 

<telerik:RadMenuItem runat="server" Text="Forms">

 

 

</telerik:RadMenuItem>

 

 

<telerik:RadMenuItem runat="server" Text="Policies">

 

 

</telerik:RadMenuItem>

 

 

</Items>

 

 

</telerik:RadMenuItem>

 

 

</Items>

 

 

</telerik:RadMenu>

 

 

</div>

 

 

</form>

 

</

 

body>

 

</

 

html>

 

0
stuart
Top achievements
Rank 1
answered on 23 Jan 2009, 07:53 PM
Please disregard my last question. I mispelled a menu button.

Thanks again for your help!
Tags
Menu
Asked by
stuart
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
stuart
Top achievements
Rank 1
Share this question
or