|
-
Mar 10th, 2010, 03:55 AM
#1
Thread Starter
Addicted Member
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?
-
Mar 10th, 2010, 05:15 AM
#2
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
-
Mar 10th, 2010, 07:02 PM
#3
Thread Starter
Addicted Member
Re: Master Pages - Code Behind
 Originally Posted by gep13
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
-
Mar 11th, 2010, 02:29 PM
#4
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
-
Mar 13th, 2010, 03:51 AM
#5
Re: Master Pages - Code Behind
 Originally Posted by Claude2005
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:
Dim cm as ClaudeMaster = CType(Me.Master, ClaudeMaster) 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|