Solved

conditionally display sitemap node

Posted on 2006-07-18
5
728 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
Comment Utility
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-
Comment Utility
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-
Comment Utility
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-
Comment Utility
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-
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

771 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

11 Experts available now in Live!

Get 1:1 Help Now