Results 1 to 5 of 5

Thread: Master Pages - Code Behind

  1. #1

    Thread Starter
    Addicted Member Claude2005's Avatar
    Join Date
    Jan 2010
    Location
    Philippines
    Posts
    166

    Master Pages - Code Behind

    Hi there,

    I just began using Master Pages (MasterPage.master). I've successfully adapt its HTML and ASP contents (elements/controls/tags) to my Web pages (Default.aspx, Default2.aspx, Default3.aspx, etc.).

    My question is: How can I make use of the code behind the Master Page (MasterPage.master.vb) as a template to my other files? (Default1.aspx.vb, Default2.aspx.vb, Default3.aspx.vb, etc.)

    For example, I have this code on MasterPage.master.vb:

    Code:
    Dim sum As Integer = 3 + 2
    And I want to display the "sum" variable from the Master Page on Label1 in Default1.aspx. So I type on Default1.aspx.vb is...

    Code:
    Dim sum As Integer
    Label1.Text = sum
    Also, I want to display the "sum" variable from the Master Page on Label4 in Default2.aspx. So I type on Default2.aspx.vb is...

    Code:
    Dim sum As Integer
    Label4.Text = sum
    I know the last two set of codes doesn't cut it. It gives me an output of zero (0) to Label1 and Label4 in Default1.aspx and Default2.aspx, respectively. Can someone teach me on how to do it correctly?

  2. #2
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Master Pages - Code Behind

    Hey,

    One way, would be to create a Public Property in your master page, and then access this public property from your content pages.

    You can see a nice example of this approach in this article:

    http://odetocode.com/Articles/450.aspx

    Hope that helps!

    Gary

  3. #3

    Thread Starter
    Addicted Member Claude2005's Avatar
    Join Date
    Jan 2010
    Location
    Philippines
    Posts
    166

    Re: Master Pages - Code Behind

    Quote Originally Posted by gep13 View Post
    Hey,

    One way, would be to create a Public Property in your master page, and then access this public property from your content pages.
    Hmm, it seems it's just like creating a separate class file in the App_Code folder. I'll try reading the article (pretty long ). Thanks gary

  4. #4
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Master Pages - Code Behind

    Hey,

    Yip, it's a long article, but an interesting one :-)

    Let me know if you have any specific questions.

    Gary

  5. #5
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Master Pages - Code Behind

    Quote Originally Posted by Claude2005 View Post
    Hi there,

    I just began using Master Pages (MasterPage.master). I've successfully adapt its HTML and ASP contents (elements/controls/tags) to my Web pages (Default.aspx, Default2.aspx, Default3.aspx, etc.).

    My question is: How can I make use of the code behind the Master Page (MasterPage.master.vb) as a template to my other files? (Default1.aspx.vb, Default2.aspx.vb, Default3.aspx.vb, etc.)

    For example, I have this code on MasterPage.master.vb:

    Code:
    Dim sum As Integer = 3 + 2
    And I want to display the "sum" variable from the Master Page on Label1 in Default1.aspx. So I type on Default1.aspx.vb is...

    Code:
    Dim sum As Integer
    Label1.Text = sum
    Also, I want to display the "sum" variable from the Master Page on Label4 in Default2.aspx. So I type on Default2.aspx.vb is...

    Code:
    Dim sum As Integer
    Label4.Text = sum
    I know the last two set of codes doesn't cut it. It gives me an output of zero (0) to Label1 and Label4 in Default1.aspx and Default2.aspx, respectively. Can someone teach me on how to do it correctly?
    You can't. A master page's codebehind can be treated like the codebehind of a separate 'thing'. The only relationship between a master page and a content page is that the master page gets injected into the content page at runtime, so it's not inheritance. It's simply a relationship between them which is based around markup/UI. The master page is available in the content page, the Master property.

    The public property usually helps.

    vb Code:
    1. Dim cm as ClaudeMaster = CType(Me.Master, ClaudeMaster)
    2. Label4.Text = cm.SumProperty

Posting Permissions

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



Click Here to Expand Forum to Full Width