ASP.NET and Master page Divs

Soldato
Joined
23 Oct 2002
Posts
4,151
Location
_
I'm looking to show or hide portions of navigations by hiding their container divs in .net, and my navigation is in a master page.

How the hell can I do this without using findControl in every single page I create to show / hide the relevant Divs?

I know you can set the visibility of Divs within the aspx portion of a content page, but what I want to do is to set the visibility of a master page div from true to false using a function that's within the master page.

So within my content page, I just want to say:

Code:
divViewable("divName",false)

Any thoughts?
 
for example

Code:
<asp:Panel ID="AdminPanel" runat="server" Height="50px" Width="249px" Visible = "false">
    </asp:Panel>



Code:
        if ((string)Session["UserName"] == UserName)
        {
            AdminPanel.Visible = true;
        }

Job Done...

Stelly
 
Having you tried putting that in a master page and calling the element within the masterpage's code-behind?

I just get no intellisense, and it tells me I haven't declared it.
 
Karl said:
Having you tried putting that in a master page and calling the element within the masterpage's code-behind?

I just get no intellisense, and it tells me I haven't declared it.

Yup and it works fine for me...

Can you send me your code?? stellyuk at gmail dot com

Stelly
 
My problems continue, although it might be worth me providing a wider scope as to why I'm trying to do what I'm doing.

I have a HTML page that has a navigation that's using CSS heavily.

http://www.karl0s.com/images/misc/siteLayout.jpg

The navigation is the left bar.

I tried using Master pages, but there's a trick to this navigation that is proving difficult to get around in .NET.

I put the header and navigation in the master page, and then used content placeholders for the main content. This works okay, but I have to change states on the navigation depending what content page I am on. If I am on the homepage, the css class for the home page button in the navigation must change to a different class. Also, the menu has sub-sections that must expand and contract depending on what section of the site I am on.

This sort of thing has similarities with the tree structure control.

Each list of items is in a div.

<div headerbutton> Title</div>

<div menuList>
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
</div>

When I click on a seperate section, menuList must be hidden.



The way I wanted to get around this was to have a hideNavigation function sat in the Master page called by the content page that'd hide all the menu divs, and then my page would call the relevant div to show via a function showMenuItem() which'd also be in the master page. I thought this was a good way of laying it out, but I can't access any div items' properties within the master page from the master page's code-behind.

I thought of using a user control for a header and a user control for navigation, but it's a bit of a rubbish way of doing it from my point of view.


I'm trying to do all this in ASP.NET (VB) - What options do you think I have? I can't believe it's so difficult to do what seems like an elementary menu system in .NET that I would have no trouble doing in Classic ASP!

Any help is *extremely* appreciated.

Thanks,

Karl.
 
Karl said:
The way I wanted to get around this was to have a hideNavigation function sat in the Master page called by the content page that'd hide all the menu divs, and then my page would call the relevant div to show via a function showMenuItem() which'd also be in the master page. I thought this was a good way of laying it out, but I can't access any div items' properties within the master page from the master page's code-behind.

You can't access the div's properties by default because they don't have (nor expect to have) the "runat='server'" control tag. If you give it the 'runat' attribute, and also an 'ID' attribute, you should be able to access it in the code-behind.

To set different states based on which content page is being loaded, I would create a function in the Master Page which sets these states for you, which you can then call from the content page. Obviously, different content pages set different states.

Edit: *sometimes* the intellisense doesn't pick up newly created controls until you next start VS, but it doesn't mean that the controls won't work; it just means you have to do a tiny bit more typing and look up properties and methods in the help file to get what you want.

To be able to call these functions from a content page, you have to specify the MasterType page declaration at the top of the content page, underneath the standard page declaration. For example, if the master page for a particular content page was stored in "~/masters/default.master", the MasterType declaration would be:
Code:
<$@ MasterType VirtualPath="~/masters/default.master" %>

This tells .Net to inheirit the master page from the chosen master page file, so you can get access to it's properties and methods from a content page.

Does this help you?
 
Last edited:
Unfortunately it doesn't help at all - Not your fault because I didn't mention that I'd already tried that. I've put a runat="server" attribute in each of the Div tags that I've tried to access, and I've also tried replacing those Divs with panels instead. Neither allows me to get at the objects; in both cases it tells me that the variable needs declaring, which prevents my page from loading.

Rather than intellisense not picking it up, it's not working at all.

Could you try doing this yourself and see if you experience the same problems? Even a simple experiment should do the trick, as I simply cannot get this to work! :(
 
Karl said:
Unfortunately it doesn't help at all - Not your fault because I didn't mention that I'd already tried that. I've put a runat="server" attribute in each of the Div tags that I've tried to access, and I've also tried replacing those Divs with panels instead. Neither allows me to get at the objects; in both cases it tells me that the variable needs declaring, which prevents my page from loading.

Rather than intellisense not picking it up, it's not working at all.

Could you try doing this yourself and see if you experience the same problems? Even a simple experiment should do the trick, as I simply cannot get this to work! :(

No worries then. I have just tried it, and it works fine for me.

If you like you can send the files to me to have a look, my messenger address is in my profile.

Out of curiosity, have you declared another HTML form in the master page, in addition to the default one that is created for you?

Edit: My messenger address isn't there at all :p it's stevenhobbs39 [at] hotmail [dot] com if you want a hand.
 
elkdanger helped me solve this by pointing out something blindingly obvious - I had somehow managed to remove the header for the master page's aspx that ties it to the code-behind. A few minor fixes later, and hey-presto! Fixed!

Thanks for your input. :)
 
Back
Top Bottom