0

enter code hereI am trying to assign a value to label in a Master Page using vb.net (not my first language-lol). I have followed info from these two references on how to access content from masterpage: 1.[https://msdn.microsoft.com/en-us/library/c8y19k6h(v=vs.90).aspx][1] 2.[nullreferenceexception was unhandled by user code in Master Page

I am getting Null Reference Exception on BUT ONLY when I try to call the method I created for it from another class. It works fine when I call it from the same class the method is in. I wrote a method from the starting page/class, WebForm1:

Public Sub PageIdentity(ByVal pageId As String)  

    ' Gets a reference to a Label control inside a ContentPlaceHolder
    Dim mpContentPlaceHolder As ContentPlaceHolder

    'Dim mpTextBox As TextBox
    Dim mpLabel As Label

    mpContentPlaceHolder = CType(Master.FindControl("MainContent"), ContentPlaceHolder)*'Null Reference Exception here*

    If Not mpContentPlaceHolder Is Nothing Then

        mpLabel = CType(mpContentPlaceHolder.FindControl("lblPageIdentifier"), Label)

        If Not mpLabel Is Nothing Then

            mpLabel.Text = pageId

        End If
    End If

End Sub

when I call this function from another class in page load like this it throws null ref ex:

 Dim oWebForm1 As WebForm1 = New WebForm1()
    oWebForm1.PageIdentity("SomeText")

I must be doing something wrong in creating the instance? I tryed writing it as a Public Shared Function first but that created other problems. Can anyone help?

UPDATE: @Joey I replaced the commented line of code below as you suggested and put the sub in the master page code behind, Site1.Master.vb:

'mpContentPlaceHolder = CType(Master.FindControl("MainContent"), ContentPlaceHolder)
        mpContentPlaceHolder = CType(Me.FindControl("MainContent"), ContentPlaceHolder)

I also ensured that the page directive on other pages included:

<%@ MasterType VirtualPath = "~/Site1.Master" %> 

On a test page, I called the sub like so:

Dim _SiteMaster As MasterPage = TryCast(Me.Master, MasterPage)
        _SiteMaster.PageIdentity("SomeText")

I am getting blue squiggly line error, message: "PageIdentity is not a member of System.Web.UI.MasterPage"

The Master page inherits like this:

Public Partial Class Site1
    Inherits System.Web.UI.MasterPage

The page calling the sub inherits like this

Public Class WebForm1
    Inherits System.Web.UI.Page
Community
  • 1
  • 1
Doreen
  • 654
  • 2
  • 14
  • 33
  • I'm a bit confused about what you are asking. The `PageIdentity` sub resides in a web form called `WebForm1` and you are trying to access that sub from another web form or class? If this is the case I would recommend you put the `PageIdentity` code into the master page itself which should already be accessible by your webform and then create an instance to your master page in any web form that you need it: `Dim _siteMaster As MasterPage = TryCast(Me.Master, MasterPage)` and then `_siteMaster.PageIdentity("SomeText")`. You would also have to change `Master` to `Me` in the sub. – Joe Uhren Jan 22 '15 at 01:35
  • Duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Bjørn-Roger Kringsjå Jan 22 '15 at 05:48
  • @JoeyJoeJoeJrShabadoo I UPDATEd in my original question with results of trying your suggestion. Now I am getting an inheritance error, not sure how to fix since my page is inheriting correctly as far as I can tell..... – Doreen Jan 22 '15 at 16:37
  • 1
    My original code was a bit off because I did not know the name of your MasterPage class. Based on your updated info I would change to this: `Dim _SiteMaster As Site1 = TryCast(Me.Master, Site1)`. This way you are referencing your masterpage class which has the `PageIdentity` sub instead of a generic masterpage class – Joe Uhren Jan 22 '15 at 17:41
  • @JoeyJoeJoeJrShabadoo - the code change in your 2nd comment did the trick. I can't figure out how to mark yours as answer...? THANK YOU. For others looking for related material, I found this link to also be helpful: http://geekswithblogs.net/TakeNote/archive/2007/06/04/112966.aspx – Doreen Jan 22 '15 at 18:08
  • @Doreen I'm glad it worked! You can't accept comments as answers unfortunately, but I posted a proper answer so that others can find it more easily. – Joe Uhren Jan 22 '15 at 18:25

1 Answers1

0

Since you require the PageIdentity subroutine to be somewhat global you can move it into your master page. The master page can then be called by any page that references it. Use the following code on any page to call the PageIdentity routine:

Dim _SiteMaster As Site1 = TryCast(Me.Master, Site1)
_SiteMaster.PageIdentity("SomeText")
Joe Uhren
  • 1,292
  • 1
  • 13
  • 26