Wrox Programmer Forums
|
BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3
This is the forum to discuss the Wrox book Beginning ASP.NET 3.5: In C# and VB by Imar Spaanjaars; ISBN: 9780470187593
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 19th, 2010, 06:58 PM
Authorized User
 
Join Date: Mar 2010
Posts: 14
Thanks: 3
Thanked 0 Times in 0 Posts
Default Selective Display of information on a panel

Hi Imar,

I'm really enjoying working through your book. I'm moving from .asp to asp.net and so far mostly doing ok working through your text and examples.

Right now, I'm trying to display or not display some links depending on which page is currently being browsed. In the MasterPage, I dropped a panel into the SideBar Div and put my text on the panel.

Then in the code behind for the MasterPage, I have

Code:
Partial Class MasterPage
    Inherits System.Web.UI.MasterPage

    Public DisplayPanel As Boolean

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If DisplayPanel Then
            Panel1.Visible = True
        Else
            Panel1.Visible = False
        End If
    End Sub
End Class
Then I try to set the logic up in the individual pages like this
Code:
Partial Class AboutUs
    Inherits System.Web.UI.Page

    Public DisplayPanel As Boolean

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        DisplayPanel = False

    End Sub
End Class
I thought I could change the DisplayPanel variable to True or False in each page's code behind page depending on whether or not I want the Panel and links on it to be visible.

Except, if the MasterPage.vb says "Public DisplayPanel As Boolean = True" then the pages ALWAYS show the panel, no matter what I set DisplayPanel to in the individual pages. And if I have "Public DisplayPanel As Boolean" or "Public DisplayPanel As Boolean = False" in the MasterPage.vb, then the panel NEVER shows up.

Have any hints for me?

Thanks,

John
 
Old March 20th, 2010, 06:14 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi John,

Take a look at this:
Code:
 
Partial Class AboutUs
    Inherits System.Web.UI.Page
  Public DisplayPanel As Boolean
  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    DisplayPanel = False
  End Sub
End Class
What do you think this code does? Override the DisplayPanel of the Master Page? In that case; nope it doesn't. It simply creates *another* boolean inside the Content Page that in no way is associated with the one in the Master Page. If you want a property or field on a Master Page that you can control from a content page, you need to tell the content page the type of the Master Page so you can access its members. Try something like this:

1. Use the same Master Page as you have now with the public DisplayPanel field in it.

2. Create a content page based on this Master Page and add the following MasterType reference to it:

Code:
 
<%@ Page Title="" Language="VB" MasterPageFile="~/MasterPage.master" 
       AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<%@ MasterType VirtualPath="~/MasterPage.master" %>
Make sure you adjust the MasterPageFile and VirtualPath properties to point to your Master Page.

3. The Master property of your content page is now strongly typed to your specific Master Page, enabling you to do something like this in the Content Page:

Code:
 
Me.Master.DisplayPanel = False ' Or True
This should do the trick and hide or show the panel based on the value you pass to Me.Master.DisplayPanel.

A few other observations: Instead of a field, you can also use a property or even a ViewState property as explained in the bok. And you can collapse the If statement to this if you want:

Panel1.Visible = DisplayPanel

although you may find your expanded version easier to read.

Hope this helps,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old March 20th, 2010, 10:58 AM
Authorized User
 
Join Date: Mar 2010
Posts: 14
Thanks: 3
Thanked 0 Times in 0 Posts
Default

Thanks Imar,

To answer your first question, yes. I thought that by declaring the variable public in the Master Page and then in the page based on the Master Page declaring the "same" variable public again that what I wanted to do would work. Guess I need to do more reading and coding/playing around.

Thanks for your suggestions. I'm off to have a cup of coffee and implement them.

John
 
Old March 20th, 2010, 11:19 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi John,

Although at runtime the content of the master page is merged with that of the content page, the two are actually two separate classes, and their class definition is not merged.

It's like "merging" a zebra and a tiger. You may end up with a zoo. But if you added a NumberOfStripes property to the Zebra class, it doesn't end up as a property on the Tiger class ;-)

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old March 20th, 2010, 12:52 PM
Authorized User
 
Join Date: Mar 2010
Posts: 14
Thanks: 3
Thanked 0 Times in 0 Posts
Default

Imar,

And it also works better when all the parts match. I followed your steps and it sort of worked. Some pages showed or didn't show the panel as planned, other pages showed it when they shouldn't....

Then I saw that in the top properties line of AboutMe.aspx, (and several other pages), where it should have been
Code:
<%@ Page Title="" Language="VB" MasterPageFile="~/MasterPages/MasterPage.master" AutoEventWireup="false" CodeFile="AboutMe.aspx.vb" Inherits="_Default" %>
the CodeFile= said CodeFile="BookInfo.aspx.vb"

I don't know how or when it was changed, I've been creating new pages by right-clicking on the project name in the explorer window not by copying and pasting files, but even I can finally figure out that it wouldn't matter how much code I changed in BookInfo.aspx.vb if the reference was to AboutMe.aspx.me.

Thank you again.

John
 
Old March 21st, 2010, 05:07 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi John,

The only time I see this happening is indeed when copying files or when renaming them outside of Visual Studio.

Oh well, it's working now..... ;-)

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!





Similar Threads
Thread Thread Starter Forum Replies Last Post
Dynamically adding a panel to an existing panel? Thorshix Java GUI 0 February 20th, 2010 02:59 PM
Chapter 4 Pg 117 Try It Out Panel Display Question workib BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 1 April 8th, 2008 01:06 AM
Problem with selective counting still_learning XSLT 1 March 22nd, 2007 06:27 PM
ViewState problem when adding a panel to a panel koekie17 C# 3 February 20th, 2006 09:17 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.