Solved

conditionally display sitemap node

Posted on 2006-07-18
5
731 Views
Last Modified: 2008-01-09
I have a user control that displays navigation links from the sitemap file:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="TopNavigationBar.ascx.cs" Inherits="UserControls_TopNavigationBar" %>
<asp:Repeater runat="server" ID="siteMapAsBulletedList" DataSourceID="SiteMapDataSource1">
    <HeaderTemplate>
        <ul>
            <li><asp:HyperLink runat="server" ID="lnkHome" NavigateUrl='<%# SiteMap.RootNode.Url %>' Text='<%# SiteMap.RootNode.Title %>'></asp:HyperLink></li>
    </HeaderTemplate>
    <ItemTemplate>
        <li>
            <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# Eval("Url") %>' Text='<%# Eval("Title") %>'></asp:HyperLink>
        </li>
    </ItemTemplate>
    <FooterTemplate>
        </ul>
    </FooterTemplate>
</asp:Repeater>
<asp:SiteMapDataSource ShowStartingNode="false" ID="SiteMapDataSource1" runat="server" />

In my code behind, I want to conditionally display specific nodes:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class UserControls_TopNavigationBar : System.Web.UI.UserControl

{

    protected void Page_Load(object sender, EventArgs e)
    {
         SiteMapNode node = e.Item.DataItem as SiteMapNode ;
         string display = node["DisplayOnLeft"];
         if( string.IsEmptyOrNull( display ) || display != "true" ) {
         e.Item.Visible = false ;
    }
   
}

I recieved the following errors:

'System.EventArgs' does not contain a definition for 'Item'
'string' does not contain a definition for 'IsEmptyOrNull'
'System.EventArgs' does not contain a definition for 'Item'

Any help is appreciated.
0
Comment
Question by:-Dman100-
  • 4
5 Comments
 
LVL 6

Accepted Solution

by:
e1v earned 500 total points
ID: 17136500
Create a ItemCreated event handler for your Repeater and put the code from Page_Load there.

And it's string.IsNullOrEmpty, not string.IsEmptyOrNull

Also, you should put the code after
SiteMapNode node = e.Item.DataItem as SiteMapNode;
in an if (node!=null) {  } block

hope this helps...
0
 

Author Comment

by:-Dman100-
ID: 17138628
Thanks for replying to my post.  I used the ItemCreated event as follows:

user control:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="TopNavigationBar.ascx.cs" Inherits="UserControls_TopNavigationBar" %>
<asp:Repeater runat="server" ID="siteMapAsBulletedList" DataSourceID="SiteMapDataSource1" OnItemCreated="Repeater1_ItemCreated">
    <HeaderTemplate>
        <ul>
            <li><asp:HyperLink runat="server" ID="lnkHome" NavigateUrl='<%# SiteMap.RootNode.Url %>' Text='<%# SiteMap.RootNode.Title %>'></asp:HyperLink></li>
    </HeaderTemplate>
    <ItemTemplate>
        <li>
            <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# Eval("Url") %>' Text='<%# Eval("Title") %>'></asp:HyperLink>
        </li>
    </ItemTemplate>
    <FooterTemplate>
        </ul>
    </FooterTemplate>
</asp:Repeater>
<asp:SiteMapDataSource ShowStartingNode="false" ID="SiteMapDataSource1" runat="server" />

code-behind:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class UserControls_TopNavigationBar : System.Web.UI.UserControl

{

    protected void Page_Load(object sender, EventArgs e)
    {
     
    }
    void Repeater1_ItemCreated(Object Sender, RepeaterItemEventArgs e)
    {
       SiteMapNode node = e.Item.DataItem as SiteMapNode ;
       string display = node["DisplayOnLeft"];
       if (string.IsNullOrEmpty(display) || display != "true")
       {
           e.Item.Visible = false;
       }
    }    
}

I received the following error when I ran the page:

'UserControls_TopNavigationBar.Repeater1_ItemCreated(object, System.Web.UI.WebControls.RepeaterItemEventArgs)' is inaccessible due to its protection level

Do I need to change the access modifier?

      
0
 

Author Comment

by:-Dman100-
ID: 17138705
I changed to ItemCreated event to:

protected void Repeater1_ItemCreated(Object Sender, RepeaterItemEventArgs e)
    {
       SiteMapNode node = e.Item.DataItem as SiteMapNode ;
       string display = node["DisplayOnLeft"];
       if (string.IsNullOrEmpty(display) || display != "true")
       {
           e.Item.Visible = false;
       }
    }    

this eliminated the protection level error, but I received the following error: 'NullReferenceException was unhandled by usercode'

This occurred on the following line:

string display = node["DisplayOnLeft"];

0
 

Author Comment

by:-Dman100-
ID: 17138956
This got it...

protected void Repeater1_ItemCreated(Object Sender, RepeaterItemEventArgs e)
    {
       SiteMapNode node = e.Item.DataItem as SiteMapNode;
         if (node!=null)
         {  
           string display = node["DisplayOnLeft"];
           if (string.IsNullOrEmpty(display) || display != "true")
           {
               e.Item.Visible = false;
           }
        }
    }  
0
 

Author Comment

by:-Dman100-
ID: 17140359
How would I create an object instance for 'node' so it isn't null when accessed in the following code:


Code:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class UserControls_TopNavigationBar : System.Web.UI.UserControl

{

    protected void Page_Load(object sender, EventArgs e)
    {
     
    }
    protected void Repeater1_ItemCreated(Object Sender, RepeaterItemEventArgs e)
    {
       SiteMapNode node = e.Item.DataItem as SiteMapNode;
           string display = node["DisplayOnLeft"];
           if (string.IsNullOrEmpty(display) || display != "true")
           {
               e.Item.Visible = false;
           }
    }    
}

I believe I need to create an object instance for node?
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now