User Control and Initialization problem

D

David Hearn

I have a user control that I have built (asp.net 1.1) which has some
functions in it to set the text of a label and some proerties of a few other
controls. My problem is that I call these functions from the page load event
of it's parent form and when I do, I get the famous "Object reference not
set to an instance of an object" error. It's almost as if everything is
working too fast. It's trying to set proprties for controls that don't exist
yet. How do I get around this?

Thanks in advance!
 
K

Karl Seguin [MVP]

It might make more sense for you to set properties, and render everything in
PreRender of your user control:

page:
page_load
myControl.Header = "Welcome!"
end load

user control:
private _header as string
public property HEader as String.
get...
set..
end property

onPrePrender()
myHeader.Text = _header
end prerender..

Karl
 
D

David Hearn

Karl,

Thanks for the reply. I tried your approach and there are no more errors.
However, I set the property and by the time that it reaches the function
that I am calling, the variable (strPageName) is Nothing again. Why is it
getting lost? Below is my code:

Private strPageName As String

Public Property PageName() As String

Get

Return strPageName

End Get

Set(ByVal Value As String)

strPageName = Value

End Set

End Property

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

If Not Page.IsPostBack Then

GetPaths()

End If

End Sub

Private Sub GetPaths()

Dim objData As New Shaw.Inet.ShawOnline.SOLCommonLibrary.Products

objData.GetPathInfo(sPath, sPathSecure)

objData = Nothing

End Sub

Public Sub SetHeaderInfo(ByVal strPageHeader As String, Optional ByVal
strMessage As String = "")

'Set table display style

Select Case strPageName

Case "CreateEDIAccount"

Me.tblHeader.Attributes.Add("class", "IndWin")

tblHeader.Width = "100%"

Case "Invoice Detail", "CreditMemo Detail"

Me.tblHeader.Attributes.Add("class", "InvDtl")

Case Else

Me.tblHeader.Attributes.Add("class", "subcat")

End Select

lblPageHeader.Text = strPageHeader

If strPageName <> "CreateEDIAccount" And strPageName <> "Sales Terms" Then

If Request.QueryString("SalesForce") <> "LogInAsTM" Then

lnkHome.Target = "_top"

lnkHome.NavigateUrl = sPathSecure & "/default.aspx"

Else

lnkHome.Target = "_top"

lnkHome.NavigateUrl = sPathSecure & "/admin/AccountList.aspx"

End If

End If

lblMessage.Text = strMessage

End Sub

Private Sub Page_PreRender(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.PreRender

SetHeaderInfo(strPageName)

End Sub



"Karl Seguin [MVP]" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME
net> wrote in message news:[email protected]...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top