How to access Menu in MasterPage from Content Page  
Author Message
Mohan Raju





PostPosted: .NET Base Class Library, How to access Menu in MasterPage from Content Page Top

Hi,

I have a Master Page having asp:Menu controls having datasource as SiteMapDataSource. Depending on the menu item clicked corresponding page loads. I changed the style of the selected menu item using StaticSelectedStyle attribute. but when i clicks any hyper link on that content page which loads another page the menu item style is changing to StaticMenuItemStyle. I want to show the style as StaticSelectedStyle.

How can i do that I tried accessing that Menu control from content pages page load function. But content page load is calling first and Master page load is calling next . So its throwing exception. Is there any solution for this

Regards,

Mohan.




.NET Development23  
 
 
Gautam Hegde





PostPosted: .NET Base Class Library, How to access Menu in MasterPage from Content Page Top

  you can do it like this

   Get the reference to the MasterPage of the page by using the Master property and then you can access any control on the masterpage

for ex:

    label tv = (Label) this.Master.FindControl("Sampleslabel");

      tv.BackColor = Color.Red;

 


 
 
Mohan Raju





PostPosted: .NET Base Class Library, How to access Menu in MasterPage from Content Page Top

I tried the following code in the page load function of content page

Menu menu = (Menu)this.Master.FindControl("Menu");

menu.Items[5].Selected = true;

this throwed index out of range exception.



 
 
Gautam Hegde





PostPosted: .NET Base Class Library, How to access Menu in MasterPage from Content Page Top

Ok, the thing is you can access the menu control and 'change its properties' whereas you cant change the properties of any of the items of the menu control, since they wont be created yet ... and they will be created only after the masterpage is loaded so you cant modify those in the contentpage

But what you can try to do is populate the items of the menu control in the content page itself and then you can modify their properties

something like

menu.datasource = your datasource

menu.databind();

menu.Items[5].Selected = true;


 
 
Mohan Raju





PostPosted: .NET Base Class Library, How to access Menu in MasterPage from Content Page Top

Yes. thats working . thanks a lot.

 
 
srem





PostPosted: .NET Base Class Library, How to access Menu in MasterPage from Content Page Top

What's wrong with setting the menu item selected to true from within the master page
Why it is not working


 
 
Mohan Raju





PostPosted: .NET Base Class Library, How to access Menu in MasterPage from Content Page Top

How can you know which menu item to select in masterpage