Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 3.5 > ASP.NET 3.5 Basics
| Search | Today's Posts | Mark Forums Read
ASP.NET 3.5 Basics If you are new to ASP or ASP.NET programming with version 3.5, this is the forum to begin asking questions. Please also see the Visual Web Developer 2008 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 3.5 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old November 30th, 2009, 11:40 AM
Banned
Points: 1,561, Level: 16
Points: 1,561, Level: 16 Points: 1,561, Level: 16 Points: 1,561, Level: 16
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jul 2005
Location: , , .
Posts: 317
Thanks: 0
Thanked 0 Times in 0 Posts
Question Can I Nest a DataList Within a LoginView's RoleGroup?

I need to know if you can nest a datalist within a LoginView's RoleGroup. I have two separate datalists, and I want to show one for admin users, and another for dept-specific users using ASP.NET Membership. Here is what I have:

Code:
<asp:LoginView ID="LoginView1" runat="server">
        <LoggedInTemplate>
            Below is a list of jobs:
        </LoggedInTemplate>
        <RoleGroups>
            <asp:RoleGroup Roles="Public Works">
                <ContentTemplate>
                    <!-- DEPT-SPECIFIC DATALIST GOES HERE -->
            <asp:DataList ID="dlJobs_depts" runat="server">
                <ItemTemplate><asp:Label ID="lblJobTitle" runat="server" /></ItemTemplate>
            </asp:DataList>
                </ContentTemplate>
            </asp:RoleGroup>
            <asp:RoleGroup Roles="Power Admin">
                <ContentTemplate>
                    <!-- ADMIN DATALIST GOES HERE -->
            <asp:DataList ID="dlJobs_admin" runat="server">
                <ItemTemplate><asp:Label ID="lblJobTitle" runat="server" /></ItemTemplate>
            </asp:DataList>
                </ContentTemplate>
            </asp:RoleGroup>
        </RoleGroups>
    </asp:LoginView>
And this is the resulting error: Compiler Error Message: BC30451: Name 'dlJobs_admin' is not declared.

So my questions are:

* Is there a way to do this?
* If not, what would be an alternative?
* If so, what am I doing wrong?

Thanks for any and all help.
  #2 (permalink)  
Old November 30th, 2009, 03:52 PM
Imar's Avatar
Wrox Author
Points: 70,322, Level: 100
Points: 70,322, Level: 100 Points: 70,322, Level: 100 Points: 70,322, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Since you can now have (or in fact, you do already have) multiple controls with the same name in different scopes, you can no longer access them directly in your code. Instead, you need to use FindControl on the LoginView to "find" your DataList control:

C#
DataList myList = LoginView1.FindControl("dlJobs_admin") as DataList;
if (myList != null)
{
// Use myList here
...
}

VB
Dim myList As DataList = TryCast(LoginView1.FindControl("dlJobs_admin"), DataList)
If myList IsNot Nothing Then
' Use myList here
End If

Untested, written in this post's message window but it should do the trick.

Cheers,

Imar

__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!


Similar Threads
Thread Thread Starter Forum Replies Last Post
DataList pbyrum ASP.NET 2.0 Basics 2 December 3rd, 2009 04:01 PM
HELP: I need to Nest Controls in a TreeNode ctt ASP.NET 2.0 Professional 0 January 4th, 2008 06:02 PM
Nest elements carlosaml XSLT 5 October 16th, 2007 01:53 AM
Datalist inside datalist amit.jagtap ASP.NET 2.0 Professional 1 September 4th, 2007 05:03 AM
nest radio button lists and postbacks lisabb ASP.NET 2.0 Basics 0 June 6th, 2007 11:41 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.