Skip to content
Loading
Master Page -CSS class change
this is my div
  • In the page load event of the page (not the master page).
  • Where I need to write this code ?
  • Same principle

    System.Web.UI.HtmlControls.HtmlGenericControl ctrl = (System.Web.UI.HtmlControls.HtmlGenericControl)masterpage.FindControl("li_1_1");
    if (ctrl != null)
    {
        // option 1
        ctrl.Attributes.Remove("class");
        ctrl.Attributes.Add("class", "class2");
        // option 2
        //ctrl.Attributes.Add("style", "background-color:yellow");
    }


    Option 1 first removes any existing stylesheet class from the control (if any) and next applies the css style defined in class2 (current_page_item in your example).

    Alternatively, you can add a style (as shown in the line that is commented out); this will (in my example) override the background-color attribute.

    Note that control is now a HtmlGenericControl instead of a standard control.
  • Sorry Sir,

    What I meant was different.......

    Please find attachment.....
    As In attachment , for each page I have to highlight current page (I am using image for that)
    in css class "current_page_item "  I had write code for it. 

    For example : If I am logcall page , it will be highlighted 
    and If i click on other link report it will navigated to report page .
    now I want to highlight report page ,But I dont know how to do it 
    Now I am using different master page to each page , I need to avoid it

    I believe now it is clarified 
    Please help me out .......


    Thank you in advance 
  • Quick example of the second approach; my masterpage is called dsrp.master.

    The markup that I used in the masterpage

           

                 
    • hallo

    •        


    The code in default.aspx.cs page_load

            // determine the masterpage of the page
            dsrp masterpage = (dsrp)Page.Master;

            // find menu item to hide
            Control ctrl =  masterpage.FindControl("li_1_1");
            if (ctrl != null)
            {
                // hide it
                ctrl.Visible = false;
            }
  • If I understand you correctly, you want Report.aspx to have a different menu from other pages (without the 'about us').

    Simplest would be to give Reports.aspx its own master page where 'about us' is missing; it defeats the purpose of using masterpages a little, but I personally consider this acceptable (as long as you don't end up with tens or hundreds of masterpages).
    Alternative is to give your menu items IDs and hide what you don't need from the code behind.

    I have not tested either approach.