{"id":11,"date":"2007-04-13T10:44:00","date_gmt":"2007-04-13T14:44:00","guid":{"rendered":"http:\/\/warriormill.com\/?p=11"},"modified":"2009-04-09T08:41:01","modified_gmt":"2009-04-09T12:41:01","slug":"building-a-menu-control-that-takes-a-sitemapdatasource-part-3","status":"publish","type":"post","link":"https:\/\/warriormill.com\/2007\/04\/building-a-menu-control-that-takes-a-sitemapdatasource-part-3\/","title":{"rendered":"Building a menu control that takes a SiteMapDataSource .. Part 3"},"content":{"rendered":"

I know i been away but here is part three, this version checks your Asp_net role and the roles for a current node. Veri simple but the menu looks very cool<\/p>\n

[code lang=”csharp”]
\n[Serializable()]
\npublic class CssMenu : Panel
\n{
\nprivate SiteMapDataSource xds;
\nprivate string[] userRoles;<\/p>\n

private bool HasRoles(SiteMapNode item)
\n{<\/p>\n

bool retval = false;
\nforeach (string item2 in item.Roles)
\n{
\nif (item2.ToString().Equals(“*”))
\nretval = true;
\nforeach (string item1 in userRoles)
\n{
\nif (item1.Equals(item2.ToString()))
\nretval = true;
\n}
\n}
\nreturn retval;
\n}<\/p>\n

public SiteMapDataSource DataSource
\n{
\nget
\n{
\nreturn xds;
\n}<\/p>\n

set
\n{
\nxds = value;
\n}
\n}
\n\/\/\/
\n\/\/\/
\n\/\/\/
\npublic string CssId
\n{
\nget
\n{
\nif (ViewState[“CssId”] == null)
\nViewState[“CssId”] = “”;
\nreturn (string)ViewState[“CssId”];
\n}
\nset
\n{
\nViewState[“CssId”] = value;
\n}
\n}
\n\/\/\/
\n\/\/\/
\n\/\/\/
\npublic bool StartAtRoot
\n{
\nget
\n{
\nif (ViewState[“StartAtRoot”] == null)
\nreturn false;
\nreturn (bool)ViewState[“StartAtRoot”];
\n}
\nset
\n{
\nViewState[“StartAtRoot”] = value;
\n}
\n}
\n\/\/\/
\n\/\/\/
\n\/\/\/
\npublic string CssCurrentClass
\n{
\nget
\n{
\nif (ViewState[“CssCurrentClass”] == null)
\nViewState[“CssCurrentClass”] = “”;<\/p>\n

return (string)ViewState[“CssCurrentClass”];
\n}
\nset
\n{
\nViewState[“CssCurrentClass”] = value;
\n}
\n}
\n\/\/\/
\n\/\/\/
\n\/\/\/
\npublic int ChildLevels
\n{
\nget
\n{
\nif (ViewState[“ChildLevels”] == null)
\nViewState[“ChildLevels”] = 1;<\/p>\n

return (int)ViewState[“ChildLevels”];
\n}
\nset
\n{
\nViewState[“ChildLevels”] = value;
\n}
\n}
\n\/\/\/
\n\/\/\/
\n\/\/\/
\nprivate void LoaChildsNodes(SiteMapNodeCollection smnc, ref Panel obj)
\n{
\nLiteral lit = new Literal();
\nlit.Text = ” “;
\n obj.Controls.Add(lit);
\n foreach (SiteMapNode item in smnc)
\n {
\n \/\/if (this.HasRoles(item.Roles))
\n \/\/ {
\n lit = new Literal();
\n lit.Text = “* “;
\n obj.Controls.Add(lit);
\n HyperLink lnk = new HyperLink();
\n lnk.Text = item.Title;
\n lnk.NavigateUrl = item.Url;
\n if (Page.Request.RawUrl.ToLower().Contains(item.Url.ToLower()) && (CssCurrentClass.Length > 0))
\n lnk.CssClass = CssCurrentClass;
\n else
\n lnk.CssClass = “”;
\n obj.Controls.Add(lnk);
\n lit = new Literal();
\n lit.Text = ” “;
\n obj.Controls.Add(lit);
\n \/\/ }<\/p>\n

}
\n lit = new Literal();
\n lit.Text = “”;
\nobj.Controls.Add(lit);
\n}
\nprotected override void OnDataBinding(EventArgs e)
\n{
\nbase.Controls.Clear();
\nbase.OnDataBinding(e);
\nSiteMapNodeCollection smnc;
\nuserRoles = Roles.GetRolesForUser();
\nxds = this.DataSource;
\nif (StartAtRoot)
\nsmnc = xds.Provider.GetChildNodes(xds.Provider.RootNode);
\nelse
\nsmnc = xds.Provider.GetChildNodes(xds.Provider.CurrentNode);<\/p>\n

Literal lit = new Literal();
\nlit.Text = “”;
\n base.Controls.Add(lit);<\/p>\n

foreach (SiteMapNode item in smnc)
\n {
\n if (HasRoles(item))
\n {
\n lit = new Literal();
\n lit.Text = ” * “;
\n base.Controls.Add(lit);
\n HyperLink lnk = new HyperLink();
\n lnk.Text = item.Title;
\n lnk.NavigateUrl = item.Url;
\n if (Page.Request.RawUrl.ToLower().Contains(item.Url.ToLower()) && (CssCurrentClass.Length > 0))
\n {
\n lnk.CssClass = CssCurrentClass;
\n }
\n else
\n {
\n lnk.CssClass = “”;
\n }
\n base.Controls.Add(lnk);
\n SiteMapNodeCollection smc2 = xds.Provider.GetChildNodes(item);
\n if ((this.ChildLevels > 1) && (smc2.Count > 0))
\n {<\/p>\n

Panel pn = new Panel();
\n lit = new Literal();
\n lit.Text = “”;
\n pn.Controls.Add(lit);
\n this.LoaChildsNodes(xds.Provider.GetChildNodes(item), ref pn);
\n lit = new Literal();
\n lit.Text = “”;
\n pn.Controls.Add(lit);
\n base.Controls.Add(pn);
\n }
\n lit = new Literal();
\n lit.Text = “”;
\n base.Controls.Add(lit);
\n }
\n }
\n lit = new Literal();
\n lit.Text = “”;
\nbase.Controls.Add(lit);<\/p>\n

}<\/p>\n

}
\n[\/code]<\/p>\n","protected":false},"excerpt":{"rendered":"

I know i been away but here is part three, this version checks your Asp_net role and the roles for a current node. Veri simple but the menu looks very cool [code lang=”csharp”] [Serializable()] public class CssMenu : Panel { private SiteMapDataSource xds; private string[] userRoles; private bool HasRoles(SiteMapNode item) { bool retval = false; […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[49],"tags":[60,62],"_links":{"self":[{"href":"https:\/\/warriormill.com\/wp-json\/wp\/v2\/posts\/11"}],"collection":[{"href":"https:\/\/warriormill.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/warriormill.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/warriormill.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/warriormill.com\/wp-json\/wp\/v2\/comments?post=11"}],"version-history":[{"count":6,"href":"https:\/\/warriormill.com\/wp-json\/wp\/v2\/posts\/11\/revisions"}],"predecessor-version":[{"id":77,"href":"https:\/\/warriormill.com\/wp-json\/wp\/v2\/posts\/11\/revisions\/77"}],"wp:attachment":[{"href":"https:\/\/warriormill.com\/wp-json\/wp\/v2\/media?parent=11"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/warriormill.com\/wp-json\/wp\/v2\/categories?post=11"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/warriormill.com\/wp-json\/wp\/v2\/tags?post=11"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}