SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Evangelist Muller2's Avatar
    Join Date
    Feb 2005
    Location
    Oxford, UK
    Posts
    522
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    master pages/classes accessing

    I have a master page and on that is a UL. the UL and all the LI's have runat="server" and a unique ID set. I can use the following code on the master page code behinde to add a styling.

    Code:
    navNewsletter.Attributes.Add("class", "active");
    *note: where navNewsletter is the ID of the LI.


    What I need to do is to be able to set this upon the page code behind that is inheriting the master page.

    How can I do this?

    I did try making a class, that was like this:

    tabs.cs
    Code:
    namespace tabs
    {
        public class tabs21
        {
            // Declare Privates
            private static string _navTabs;
    
            public static string navTabsSet
            {
                set
                {
                    _navTabs = value;
                }
            }
    
            public static string navTabsReturn
            {
                get { return _navTabs; }
            }
    
            public static string returnTabID()
            {
                return tabs21.navTabsReturn;
            }
    
        }
    }
    which I could then 'set; the value from the page codebehind using this code:
    default.aspx.cs
    Code:
        protected void Page_Load(object sender, EventArgs e)
        {
            SetPageDetails();
        }
        private void SetPageDetails()
        {
            tabs21.navTabsSet = "navNewsletter";
        }
    Then on the master page code behind I tried pulling through the value using this:
    Code:
        protected void Page_Load(object sender, EventArgs e)
        {
            SetUserControlVariables();
        }
    
        private void SetUserControlVariables()
        {
    
            string bob = tabs21.returnTabID();
            bob.Attributes.Add("class", "active");
            
            
        }
    But that won't compile a there isn't Attributes of string (bob).

    I can response.write with the bob variable and the output is correct, but instead of writting the output I need to run it.

    I hope that makes sense, but if someone could help out that would be fantastic.

    Cheers,

    Al

  2. #2
    SitePoint Wizard
    Join Date
    Feb 2007
    Posts
    1,274
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can expose the LIs on the master page by simple property getters, such as

    Code:
    public HtmlGenericControl NavNewsletter { get{ return navNewsletter; } }
    The content page can access its master page using its Master property. The catch is that by default the Master property is of the generic master page type, not the specific type of your master page, thus it cannot access the property. By using the @MasterType directive you can make the the Master property type specific:

    Code:
    <&#37;@ MasterType VirtualPath="~/MasterPage.master”" %>
    Now you can simply manipulate the property using this code on the content page:

    Code:
    Master.NavNewsletter.Attributes["class"] = "active";

    ----

    Having said this, I believe that you are trying to indicate the "active" node (or active branch?) in a menu or other navigational element.

    If so, there may be other better ways of accomplishing this. Consider using the sitemap. This will allow the masterpage to automatically indicate the active node or branch, based on the url of the actual content page.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •