Solved

conditionally display sitemap node

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
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.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

775 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