Solved

conditionally display sitemap node

Posted on 2006-07-18
5
745 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-
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

696 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