Adding Custom Attributes to the Site Map Nodes so they can be used in the MenuControl Adapter

Jul 29, 2007 at 11:06 PM
Hi,
I'm new at this VS C# coding (having been used to C for many years) environment and so this question that I’m about to ask may be inappropriate for this forum but I’m hoping you will be able to answer the question or point me in the right direction.

All I'm trying to do is extend the CSS MenuAdapter.cs so that I can include a new Site Map Node custom attribute so in turn I can change the html generated by the MenuAdapter.cs to include a new attribute within the <a tag

i.e

Site Map Node
<siteMapNode url="~/GenericPage.aspx?goto=Products" title="Products" description="Create a Fundraising Page" mid="m1" />

mid attribute assigned to the <a id= tag
<a id=”m1” href="/CssAdaptersTutorial1/GenericPage.aspx?goto=SupportKnowledgeBase" class="AspNet-Menu-Link" title="Knowledge Base">Knowledge Base</a>

I then use the <a id= tag to help render my CSS rules with a little bit more control.

What I don’t know how to do is how to pull through or reference the new mid Site Map Node custom attribute within the CSS Friendly Control Adapters MenuAdapter.cs code?

Any snippets of code etc. would be much appreciated.
Jul 31, 2007 at 3:12 PM
You may have more success getting a response by posting on the CSS adapter forums at http://forums.asp.net/1018.aspx
Aug 1, 2007 at 2:59 AM


bdemarzo wrote:
You may have more success getting a response by posting on the CSS adapter forums at http://forums.asp.net/1018.aspx


Thanks.
Aug 21, 2007 at 3:12 PM
Edited Aug 21, 2007 at 3:14 PM
I can't find a post concerning this question on forums.asp.net, so i'll post an answer here ;-).

The custom attribute isn't readily available to you on the menuitem, I think it's only available during databinding. So if it's an attribute for node selection, use it in a custom sitemap provider. If it has to be written to the output, use the MenuItemDataBound event to add it to the menuitem. In the buildItem method you use writer.WriteAttribute("id", item.PROPERTY).
Because you're dealing with menuitems you have to use an existing property (correct me if i'm wrong!)

and the databinding code:
protected override void OnInit(EventArgs e)
{
menu.MenuItemDataBound += new MenuEventHandler(BindCustomAttributes);
base.OnInit(e);
}

protected void BindCustomAttributes(object sender, MenuEventArgs e)
{
SiteMapNode sn = e.Item.DataItem as SiteMapNode;
if (!String.IsNullOrEmpty(sn["mid"]))
{
e.Item.PROPERTY= sn["mid"];
}
}

Hope this helps,
rinze
Aug 22, 2007 at 11:21 AM
Edited Aug 22, 2007 at 11:40 AM
Hi Rinze,
Thanks for taking a look at this for me. However I must still be missing something fundamental as I'm getting these errors when I compile

Error 3 The name 'menu' does not exist in the current context C:\Inetpub\wwwroot\CssAdaptersTutorial1\App_Code\Adapters\MenuAdapter.cs 35 17 C:\...\CssAdaptersTutorial1\
Error 4 'System.Web.UI.WebControls.MenuItem' does not contain a definition for 'PROPERTY' C:\Inetpub\wwwroot\CssAdaptersTutorial1\App_Code\Adapters\MenuAdapter.cs 44 24 C:\...\CssAdaptersTutorial1\
Error 6 'System.Web.UI.WebControls.MenuItem' does not contain a definition for 'item' C:\Inetpub\wwwroot\CssAdaptersTutorial1\App_Code\Adapters\MenuAdapter.cs 176 67 C:\...\CssAdaptersTutorial1\

I placed your code into the file
C:\Inetpub\wwwroot\CssAdaptersTutorial1\App_Code\Adapters\MenuAdapter.cs

placing the writer.WriteAttribute("id", item.PROPERTY) in the BuildItem method

This is my OnInit method in the same physical file:

protected override void OnInit(EventArgs e)
{
base.OnInit(e);
if (Extender.AdapterEnabled)
{
RegisterScripts();
menu.MenuItemDataBound += new MenuEventHandler(BindCustomAttributes);
}
}

Cheers,
Dave

PS I have posted this item to the sugested forum but it has not been published by the moderator just yet
Aug 22, 2007 at 3:21 PM
Edited Aug 22, 2007 at 3:27 PM
Hi Dave,

my bad. Menu is a property I use that casts the control to a menu type. It is not included in the code sample here it is:

private Menu m_menucontrol = null;
public virtual Menu menu
{
get
{
if (m_menucontrol == null)
{
m_menucontrol = Control as Menu;
}
return m_menucontrol;
}
}

U can leave out the virtual keyword. I have implemented it this way because now I overload my Control Adapter to seperate 'Cleaning up' the .net control output from adding my Layout (i.e. class definitions). I only want to cast the control once, so I control it in the property.

Please note that 'PROPERTY' is a placeholder. Replace it with the menuitem property you wish to assign the value to.

In regard to error 6: not sure i'm getting that one. The method definition private void BuildItem(MenuItem item, HtmlTextWriter writer) has a menuitem parameter which you should be able to use.

Hope this helps,
rinze