/// <summary> /// Returns the Child items wrapped in an HTML table, with correctly formatted /// TR and TD elements /// </summary> /// <param name="items"><see cref="NavBarItem"/> Collection to render</param> /// <param name="blockname">The heading that appaears at the top of the block</param> /// <param name="selectedcell">The ID of the currently selected cell (so that it can be rendered /// with the correct css class)</param> /// <returns>An HTMLTable containing al of the items</returns> public System.Web.UI.HtmlControls.HtmlTable GetChildrenAsTable(NavBarItems items, string blockname, string selectedcell) { System.Web.UI.HtmlControls.HtmlTable tbl = new System.Web.UI.HtmlControls.HtmlTable(); tbl.Border = 0; tbl.CellPadding = 2; tbl.CellSpacing = 0; tbl.Width = "100%"; bool allowed = !_useroles; int itmcount = 0; foreach (NavBarItem itm in items) { allowed = false; if (!this.DesignMode) { if (itm.Roles == null || itm.Roles.Equals(string.Empty)) { allowed = !_useroles; } else if (itm.Roles != null) { switch (itm.Roles) { case "*": allowed = true; break; case "?": allowed = this.Page.User.Identity.IsAuthenticated; break; default: string[] roles = Convert.ToString(itm.Roles).Split(','); for (int i = 0; i < roles.Length; i++) { if (this.Page.User.IsInRole(roles[i])) { allowed = true; break; } } break; } } } else { allowed = true; } if (allowed) { string cellid = blockname + "_nbNavItem_" + (++itmcount).ToString(); string selectedstyle = string.Empty; string basestyle = string.Empty; string hoverstyle = string.Empty; if (itm.ItemStyle == null || itm.ItemStyle.Selected == null) { if (this.DefaultItemStyle == null || this.DefaultItemStyle.Selected == null) { selectedstyle = string.Empty; } else { selectedstyle = this.DefaultItemStyle.Selected; } } else { selectedstyle = itm.ItemStyle.Selected; } if (itm.ItemStyle == null || itm.ItemStyle.Base == null) { if (this.DefaultItemStyle == null || this.DefaultItemStyle.Base == null) { basestyle = string.Empty; } else { basestyle = this.DefaultItemStyle.Base; } } else { basestyle = itm.ItemStyle.Base; } if (itm.ItemStyle == null || itm.ItemStyle.Hover == null) { if (this.DefaultItemStyle == null || this.DefaultItemStyle.Hover == null) { hoverstyle = string.Empty; } else { hoverstyle = this.DefaultItemStyle.Hover; } } else { hoverstyle = itm.ItemStyle.Hover; } if (selectedcell != string.Empty) { if (selectedcell != cellid) { itm.Selected = false; } else { itm.Selected = true; } } HtmlTableRow tr = new HtmlTableRow(); string style = string.Empty; if (itm.Selected) { style = selectedstyle; } else { style = basestyle; } tr.Attributes.Add("class", style); tr.Style.Add("width", "100%"); HtmlTableCell td = new HtmlTableCell(); td.Style.Add("width", "100%"); td.ID = cellid; td.Attributes.Add("class", style); td.Attributes.Add("onmouseover", string.Format("nbItemHighlight(this,'{0}')", hoverstyle)); td.Attributes.Add("onmouseout", string.Format("nbItemLowlight(this,'{0}')", style)); td.Attributes.Add("selected", itm.Selected.ToString()); if (_usereplace) { td.Attributes.Add("onclick", string.Format("nbReplace('{0}', '{1}', '{2}')", cellid, (itm.NavigateUrl == null || itm.NavigateUrl.Equals(string.Empty)) ? "#" : System.Web.HttpUtility.UrlEncode(itm.NavigateUrl), selectedstyle)); } else { td.Attributes.Add("onclick", string.Format("nbNavigate('{0}', '{1}', '{2}')", cellid, (itm.NavigateUrl == null || itm.NavigateUrl.Equals(string.Empty)) ? "#" : System.Web.HttpUtility.UrlEncode(itm.NavigateUrl), selectedstyle)); } if (itm.Description != null && itm.Description.Length > 0) { td.Attributes.Add("title", System.Web.HttpUtility.HtmlEncode(itm.Description)); } HtmlAnchor a = new HtmlAnchor(); LiteralControl lc = new LiteralControl(); a.HRef = string.Format("javascript: {1}('{0}', '{2}', '{3}');", cellid, _usereplace ? "nbReplace" : "nbNavigate", (itm.NavigateUrl == null || itm.NavigateUrl.Equals(string.Empty)) ? "#" : System.Web.HttpUtility.UrlEncode(itm.NavigateUrl), selectedstyle); StringBuilder inner = new StringBuilder(); if (itm.Image != null && itm.Image.Length > 0) { inner.AppendFormat("<img src='{0}'/>", itm.Image); } inner.Append("<span class='nbItem'>" + itm.Text + "</span>"); lc.Text = inner.ToString(); a.InnerHtml = inner.ToString(); td.Controls.Add(lc); tr.Cells.Add(td); tbl.Rows.Add(tr); } } return(tbl); }
/// <summary> /// Gets the items wrapped in a table /// </summary> /// <param name="nb">The Navbar in which rendering is taking place</param> /// <param name="items">The items to render into the group</param> /// <returns></returns> private string GetChildrenAsTable(NavBar nb, NavBarItems items) { System.Web.UI.HtmlControls.HtmlTable tbl = new System.Web.UI.HtmlControls.HtmlTable(); tbl.Border = 0; tbl.CellPadding = 2; tbl.CellSpacing = 0; tbl.Width = "100%"; foreach (NavBarItem itm in items) { HtmlTableRow tr = new HtmlTableRow(); string style = string.Empty; if (itm.ItemStyle == null || itm.ItemStyle.Base == null) { if (nb.DefaultItemStyle == null || nb.DefaultItemStyle.Base == null) { style = string.Empty; } else { style = nb.DefaultItemStyle.Base; } } else { style = itm.ItemStyle.Base; } string hoverstyle = string.Empty; if (itm.ItemStyle == null || itm.ItemStyle.Hover == null) { if (nb.DefaultItemStyle == null || nb.DefaultItemStyle.Hover == null) { hoverstyle = string.Empty; } else { hoverstyle = nb.DefaultItemStyle.Hover; } } else { hoverstyle = itm.ItemStyle.Hover; } tr.Attributes.Add("class", style); tr.Style.Add("width", "100%"); HtmlTableCell td = new HtmlTableCell(); td.Style.Add("width", "100%"); td.Attributes.Add("class", style); td.Attributes.Add("onmouseover", string.Format("nbItemHighlight(this,'{0}')", hoverstyle)); td.Attributes.Add("onmouseout", string.Format("nbItemLowlight(this,'{0}')", style)); HtmlAnchor a = new HtmlAnchor(); a.HRef = itm.NavigateUrl; StringBuilder inner = new StringBuilder(); if (itm.Image != null && itm.Image.Length > 0) { inner.AppendFormat("<img src='{0}'/><br>", itm.Image); } inner.Append(itm.Text); a.InnerHtml = inner.ToString(); td.Controls.Add(a); tr.Cells.Add(td); tbl.Rows.Add(tr); } StringBuilder sb = new StringBuilder(); TextWriter tw = new StringWriter(sb); HtmlTextWriter htw = new HtmlTextWriter(tw); tbl.RenderControl(htw); return(sb.ToString()); }