I Have made a small User
control called UCTextBox.ascx and
it contains one text box and one button. When button is clicked it updates the
test in the text box. It also contains one small public sub ChangeTextBox
to update the text box. Here is the VB code behind:-
Public Class
UCTextBox
Inherits System.Web.UI.UserControl
'THIS BUUTON INSIDE THE USER CONTROL
DOES CHANGE TEXTBOX1
Private Sub UCButton_Click(sender As Object, e As System.EventArgs) Handles
UCButton.Click
TextBox1.Text = "Text
Change by UC button"
End Sub
'A PROCEDURE TO CHANGE TEXBOX1 FROM A
CALLING PAGE
Public Sub
ChangeTextBox()
TextBox1.Text = "Text
Altered by Calling Page"
End Sub
End Class
I then us the user control
in a web page called CallingPage.aspx. In this calling page is another
botton that when clicked it attempts to call the ChangeTestBox sub in the
UserControl. Here is the VB code behind:-
Public Class
CallingPage
Inherits System.Web.UI.Page
'THIS BUTTON CALLS A METHOD IN THE USER
CONTROL
Private Sub CallButton_Click(sender As
Object, e As
System.EventArgs) Handles CallButton.Click
Dim Control As UCTextBox = New UCTextBox
Control.ChangeTextBox()
End Sub
End Class
When I debug (run) the
CallingPage and press the button that belongs to the UserControl the text box
is updated okay.
However, when I press the
button that part of the CallingPage it calls the sub UpdateTextbox but
throws an error:- NullReferenceException was unhandled by user code – Object
reference not set to an instant of an object.
I don’t undertand this
error as I have set an instance of UCTextBox. This error seems peculiar to
ASP.Net and VB.Net. I have used user controls in a VB windows application and a
WPF web application without this error.
Can someone please show me what I can do
so that I can call method in a UserControl class from a web page and update
controls within the UserControl..
you can't ahandle it like this , you need to referenece to current control you added in the Page not Creating one , Replace this Part :
Private Sub CallButton_Click(sender As
Object, e As
System.EventArgs) Handles CallButton.Click
Dim Control As UCTextBox = New UCTextBox
Control.ChangeTextBox()
End Sub
with
Dim uc1 As ASP.webusercontrol_ascx = DirectCast(FindControl("UserControlId"), ASP.webusercontrol_ascx))
uc1.MethodName()
I have also since worked out that you can refer to the UserControl diredt by referring to the ID name on the ascx page and then refer to a method. e.g UCTextBox1.ChangeTextbox.
Bob Bown
Member
4 Points
4 Posts
NullReferenceException when calling method in a UserControl Class
Jan 05, 2012 10:30 PM|LINK
I Have made a small User
control called UCTextBox.ascx and
it contains one text box and one button. When button is clicked it updates the
test in the text box. It also contains one small public sub ChangeTextBox
to update the text box. Here is the VB code behind:-
Public Class
UCTextBox
Inherits System.Web.UI.UserControl
'THIS BUUTON INSIDE THE USER CONTROL
DOES CHANGE TEXTBOX1
Private Sub UCButton_Click(sender As Object, e As System.EventArgs) Handles
UCButton.Click
TextBox1.Text = "Text
Change by UC button"
End Sub
'A PROCEDURE TO CHANGE TEXBOX1 FROM A
CALLING PAGE
Public Sub
ChangeTextBox()
TextBox1.Text = "Text
Altered by Calling Page"
End Sub
End Class
I then us the user control
in a web page called CallingPage.aspx. In this calling page is another
botton that when clicked it attempts to call the ChangeTestBox sub in the
UserControl. Here is the VB code behind:-
Public Class
CallingPage
Inherits System.Web.UI.Page
'THIS BUTTON CALLS A METHOD IN THE USER
CONTROL
Private Sub CallButton_Click(sender As
Object, e As
System.EventArgs) Handles CallButton.Click
Dim Control As UCTextBox = New UCTextBox
Control.ChangeTextBox()
End Sub
End Class
When I debug (run) the
CallingPage and press the button that belongs to the UserControl the text box
is updated okay.
However, when I press the
button that part of the CallingPage it calls the sub UpdateTextbox but
throws an error:- NullReferenceException was unhandled by user code – Object
reference not set to an instant of an object.
I don’t undertand this
error as I have set an instance of UCTextBox. This error seems peculiar to
ASP.Net and VB.Net. I have used user controls in a VB windows application and a
WPF web application without this error.
Can someone please show me what I can do
so that I can call method in a UserControl class from a web page and update
controls within the UserControl..
call
Ahmed Moosa
Star
7784 Points
1232 Posts
Re: NullReferenceException when calling method in a UserControl Class
Jan 06, 2012 01:47 AM|LINK
Hi,
you can't ahandle it like this , you need to referenece to current control you added in the Page not Creating one , Replace this Part :
Private Sub CallButton_Click(sender As Object, e As System.EventArgs) Handles CallButton.Click Dim Control As UCTextBox = New UCTextBox Control.ChangeTextBox() End Subwith
Dim uc1 As ASP.webusercontrol_ascx = DirectCast(FindControl("UserControlId"), ASP.webusercontrol_ascx)) uc1.MethodName()MCC - MCPD -MCTS
Bob Bown
Member
4 Points
4 Posts
Re: NullReferenceException when calling method in a UserControl Class
Jan 11, 2012 06:17 AM|LINK
Thanks for the answer.
I have also since worked out that you can refer to the UserControl diredt by referring to the ID name on the ascx page and then refer to a method. e.g UCTextBox1.ChangeTextbox.
bobthebuilder