User Control Help

J

Julio Sarmiento

Can someone tell me what I am doing wrong??? Below you will find a brief
sample of code that I am using that has a User Control. I know that it is a
lot to ask but I am desperate. The error I receive is: OBJECT REFERENCE NOT
SET TO AN INSTANCE OF THE OBJECT

wuc.ascx
Public MustInherit Class wuc

Inherits System.Web.UI.UserControl

Protected WithEvents TextBox1 As System.Web.UI.WebControls.TextBox

Protected WithEvents Calendar1 As System.Web.UI.WebControls.Calendar




Public ReadOnly Property getDate() As String

Get

Return Me.TextBox1.Text

End Get

End Property



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

'Put user code to initialize the page here

End Sub



Private Sub Calendar1_SelectionChanged(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Calendar1.SelectionChanged

Me.TextBox1.Text = Me.Calendar1.SelectedDate

End Sub

End Class



HERE IS THE ASPX

Public Class WebForm5

Inherits System.Web.UI.Page

Protected WithEvents TextBox1 As System.Web.UI.WebControls.TextBox

Protected WithEvents Button1 As System.Web.UI.WebControls.Button

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

'Put user code to initialize the page here

End Sub



Private Sub Button1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim x As test.wuc

Me.TextBox1.Text = x.getDate()

End Sub

End Class
 
M

Murgi

Julio Sarmiento said:
Can someone tell me what I am doing wrong??? Below you will find a brief
sample of code that I am using that has a User Control. I know that it is a
lot to ask but I am desperate. The error I receive is: OBJECT REFERENCE NOT
SET TO AN INSTANCE OF THE OBJECT

OK, 2$es
 
J

Jon Skeet [C# MVP]

Julio Sarmiento said:
Can someone tell me what I am doing wrong??? Below you will find a brief
sample of code that I am using that has a User Control. I know that it is a
lot to ask but I am desperate. The error I receive is: OBJECT REFERENCE NOT
SET TO AN INSTANCE OF THE OBJECT

You're never actually creating an instance of TextBox and assigning a
reference to it to TextBox1. (Same goes for Calendar1.)

That means the variables will have their default null values. When you
try to dereference that null reference, you'll get the exception above.
 

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

Top