No Box Solutions
< Return to Creating Some Style article

Navbar.ascx

<%@ Control runat="server" language="c#" classname="navbar" %>
<%@ Import namespace="System.Xml" %>

<script runat="server" language="c#">

private string sourcexml = null;
private string contexturl = null;

public string SourceXml
{
  get{ return sourcexml;}
  set{ sourcexml = value;}
}

public void Page_Load(object o, EventArgs e)
{
  //make sure there is an xml file reference from the control
  if(SourceXml!=null)
  {
    contexturl = Request.Url.PathAndQuery;

    XmlTextReader navreader = new XmlTextReader(Server.MapPath(SourceXml));

    try
    {
      while(navreader.Read())
      {
        string Title = "";
        string Url = "";
        if (navreader.HasAttributes)
        {
          while (navreader.MoveToNextAttribute())
          {
            if(navreader.Name=="title")
              Title = navreader.Value;
            else if(navreader.Name=="url")
              Url = navreader.Value;
          }
        }

        if(Title!="" && Url!="")
          RenderLink(Title, Url);

        navreader.MoveToElement();
      }
    }
    catch(Exception){}
    finally
    {
      if(navreader.ReadState != ReadState.Closed)
        navreader.Close();
    }
  }
  else
    this.Visible = false;
}


public void RenderLink(string Title, string Url)
{
  Label s = new Label();
  if(Url==contexturl)
  {
    //special case - we don't want to render the current
    //pages nav item as a link
    s.BorderColor=System.Drawing.Color.FromName("Red");
    s.BorderStyle=BorderStyle.Solid;
    s.BorderWidth=Unit.Pixel(2);
    s.Controls.Add(new LiteralControl(Title));
  }
  else
  {
    HyperLink h = new HyperLink();
    h.Text = Title;
    h.NavigateUrl = Url;
    s.BorderColor=System.Drawing.Color.FromName("Black");
    s.BorderStyle=BorderStyle.Solid;
    s.BorderWidth=Unit.Pixel(2);
    s.Controls.Add(h);
  }

  NavBar.Controls.Add(s);

}

</script>

<div>
<asp:placeholder runat="server" id="NavBar"></asp:placeholder>
</div>

 

Copyright © 2008 No Box Solutions