Solved

conditionally display sitemap node

Posted on 2006-07-18
5
739 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Title # Comments Views Activity
C# Comparing String Arrays. 5 57
Graphics 2 32
VB: Convert 2 dates to specific format 24 52
How to Generate a Method from a keyboard shortcut in Visual Studio? 2 19
In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

809 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