multiple Content page, Null reference to a control with Masterpage

Hi Guys
Sorry about the long subject didnt really understand what to put in.
I have a masterpage and two content pages for this master.
THe master page has a dynamic menu that get generated at page_PreInit
The menu are in form of linkbutton. then you click a menubutton its calls sub called GetContent. this sub queryies the database to get the contents and displays them in a Literal control on content page 1.

When the use clicks the menu item 3, i have a server.transfer to contactus.aspx page, if all works fine till here, but from contactus.aspx page when i select the another menu item it though Null reference expection to the literal control

                Dim myLiteral As Literal = Me.mainContent.FindControl("pageContent")
                myLiteral.Text = myDataSet.Tables(0).Rows(0).Item(0).ToString

Any ideas on how will i refer back to contentpage1 after doing a server.transfer to contactus.aspx?
After though...
I would ideally like to store my contact us page int he database as well, btu dont really know if will be possiblet o have button click event fire in code behind when before its generated.!!!
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Firstly try using Server.Redirect instead of Server.Transfer, as that can sometimes cause strange issues.

Secondly, may I ask is there a reason that you are trying to load content into your content page via a call in the MasterPage? Wouldn't it be easier to load it from the page itself?
Also try this

Dim myLiteral As Literal = CType(Me.mainContent.FindControl("pageContent"),Literal)

However I think the problem is caused by the fact that you are trying to load the pageContent too early in the page lifecycle. (onPreInit)

I assume that when the default page is loaded it contains the pageContent control, so it is already there when you click on a menu button. However, when you are on the ContactUs page, it doesn't contain a pageContent control so you are getting a null reference exception.

You Sub is being called before the new page loads, and therefore before the pageContent control is visible to it.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mayank_AgarwalAuthor Commented:
Hi Boro_bob
Can you please elaborate on this point. this may help me ......
"Secondly, may I ask is there a reason that you are trying to load content into your content page via a call in the MasterPage? Wouldn't it be easier to load it from the page itself?"

THe master page has the sub that loads a menu which is common to all contentpages. Masterpage also has a sub at the loads the contents on each page depending on the menu item that was clicked. the difference with contact us page is that its is a seperate aspx file which i want to display using the masterpage. Since i dont know any way i can gerenerate the contact us page dynamically alongwith its events triggers that why i had to take this apporach.

The pageContent literal in on one of the contentpage.
the contactus page has no literal just textboxes and a button.

Though when you say to load content via the content page does that mean i should pass the menuitem variable to content page and have the GETCONTENT sub in the content page rather than Masterpage?

i would have ideally liked ot have a class that will hold all my sub, but i am not how that can be done???
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

I didn't realise that you weren't using the contactus page with the masterpage. One suggestion would be to open the contact us page as a pop up window.

Otherwise you will need the code that loads the pageContent to reside in the contentpages themselves. As you suggust, it would be possible to have a class that does this seperate from your web pages, and let them all call that.

As a simple example I have attached a little class called DateFormatter which has one method called GetShortDate.

To use this class from your web page would would need to add
Imports Web.Components.Formatter to the top of your code behind, and the somewhere in your code use it something like this:

Dim dtNow As DateTime = DateTime.Now
Dim todaysDate As String = GetShortDate(todaysDate)

You could add as many methods as you liked to your class and use them in a similar way. Therefore you could have a method which took a string or int (some sort of page id), and then returned your page content.

I hope you fid this helpful.
Namespace Web.Components.Formatter
    Imports System
    Imports System.Collections.Generic
    Imports System.Text
    ''' <summary>
    ''' Formatter for presenting dates
    ''' </summary>
    Public Class DateFormatter
        ''' <summary>
        ''' Short format date dd MMM yy - e.g. 10 Dec 07
        ''' </summary>
        ''' <param name="date"></param>
        ''' <returns></returns>
        Public Shared Function GetShortDate(ByVal date As DateTime) As String
            Return date.ToString("dd MMM yy")
        End Function
    End Class
End Namespace

Open in new window

Mayank_AgarwalAuthor Commented:
Thanks mate!!
Imagine this.
I have one master page and three content pages, the content pages are called contenteECC.aspx, contactus.aspx, login.aspx. they are all connected to the one master page that has the CSS for look and feel and the Menu that is common accross the site.

Issue 1: i need to be able to click the menu and determine which page to show, (i have done a sub that will do this and resposen.redirect on indivula page....) Code attached
Problem is that when the site load the first time, menu value is 0 so it throws an error.

issue 2: when i go to contact us page or parents login(form authentication returnUrl) hte masterpage backgtound and formatting shows up but NOT the menu, though the menu is shown for contentECC.aspx. I have tried both server.transfer and response.redirect no luck.  server.transfer doesnt work for formauthentication so thats out for login page.

Issue3: code attached for master page page_load, the loadmenu sub is called all the time which is not good for performance. i cant crack why it does this. I am guessing its and event issue.
When the user click a menu its stored as a session variable and passed to getcontent sub to query the database and retrive the pagecontent, this sub is in the page load of contentECC.aspx content page only. the contactus and login pages are static pages so no calls to the database are made. but they are using the masterpage.

    Public Sub decidePage(ByVal menuId As Integer)
        Using myconn As New SqlConnection(connStr)
            Dim sql As String = "select Text from menu where menuID = " & menuId
            Dim cmd As New SqlCommand(sql, myconn)
            Dim mtext As String = cmd.ExecuteScalar()
            If Strings.InStr(mtext, "Contact") > 0 Then
            ElseIf Strings.InStr(mtext, "Parent") > 0 Then
            End If
        End Using
    End Sub
        If IsNothing(Session("SiteID")) Then
            Session("SiteID") = Request.QueryString("siteID")
            siteID = Session("siteID")
        End If
        If Not IsPostBack Then  
        End If

Open in new window

Mayank_AgarwalAuthor Commented:
in the above i was able to fix the Issue that the menu does appear now but the a new drama has poped up

the home page has two links
<a href="contentECC?siteID=1>
<a href="contentECC?siteID=2>

the problem that the menu for the both site come up at the same mene for siteID 1. i have checked the variable querystring via debug, they do change from 1 or 2 based ont eh link, but the menu doesnt change same for every thing.....
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.