Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with ascx properties 1

Status
Not open for further replies.

abrewis

Programmer
Oct 16, 2001
37
0
0
GB
I am new to ASP.NET and am building an application with several UserControls. My problems lies when I try and set/return values from the ascx to an aspx page, I get the error: System.NullReferenceException: Object reference not set to an instance of an object

An example of my code to set the properties is shown below. Code-behind page for my ascx is:

Public MustInherit Class Customer

Inherits System.Web.UI.UserControl
...
...
Public Property pForename() As String
Get
Return txtForename.Text.Trim.ToString
End Get
Set(ByVal Value As String)
txtForename.Text = Value
End Set
End Property

Public Property pSurname() As String
Get
Return txtSurname.Text.Trim.ToString
End Get
Set(ByVal Value As String)
txtSurname.Text = Value
End Set
End Property
...
...
End Class

And my code-behind page for the aspx page is:

Imports System.Web.UI.UserControl

Public Class Request
Inherits System.Web.UI.Page

Protected pCustomer As Customer

#Region " Web Form Designer Generated Code "

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

pCustomer.pSurname = "Smith"
pCustomer.pForename = "John"

End Sub

End Class

I wondered if my ascx page had not yet been loaded? Any help would be greatly appreciated.

Many thanks
Alasdair

 
You'll need to declare the control using WithEvents i.e.
Code:
Public WithEvents MyUserControl As pCustomer
Note: rename "MyUserControl" with the id you have given the user control in the HTML

--------------------------------------------------------------------------------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
As easy as that......

Many thanks ca8msm
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top