stuck: I cant reach usercontrol's class ..

S

Support

in asp.net
If I have a web usercontrol WUC with code behind Class CLASSWUC
and I have a web page that loads the control

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
MANAGEGENERAL = LoadControl("WUC.ascx")
MyPlaceholder.Controls.Add(MANAGEGENERAL)
InitializeComponent()
End Sub

And if in CLASSWUC I have a finction that displays a value on the label of
WUC like

Sub DisplayValue(value as String)
Mylabel.text=value
End Sub

QUESTION:

What code shoud I have in my web page to be able to call DisplayValue so as
to display a value in the label.

I current get the message:
Line 1122: Dim stuff As New
PORTAL.ManageGeneralNameSpace.CLASSWUC
Line 1123: stuff.DisplayValue("test")
Exception Details: System.NullReferenceException: Object reference not set
to an instance of an object.

Thanks
 
K

Karl Seguin

private manageGeneral as ClassWuc
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
manageGeneral = ctype(LoadControl("WUC.ascx"), ClassWuc)
manageGeneral.DisplayValue("heh")
MyPlaceholder.Controls.Add(manageGeneral )
InitializeComponent()
End Sub

you cold call displayValue in page_load as well...the trick is that
Page.LoadControl returns a System.Web.UI.Control...which your class ClassWuc
inherits from...however, in order to get DisplayValue (which
System.Web.UI.control doesn't have) you need to cast the returned type to
the appropriate ClassWuc class

Karl


--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
 
S

Support

THANKS!!!
saved from desperation..

Karl Seguin said:
private manageGeneral as ClassWuc
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
manageGeneral = ctype(LoadControl("WUC.ascx"), ClassWuc)
manageGeneral.DisplayValue("heh")
MyPlaceholder.Controls.Add(manageGeneral )
InitializeComponent()
End Sub

you cold call displayValue in page_load as well...the trick is that
Page.LoadControl returns a System.Web.UI.Control...which your class
ClassWuc
inherits from...however, in order to get DisplayValue (which
System.Web.UI.control doesn't have) you need to cast the returned type to
the appropriate ClassWuc class

Karl


--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
 

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
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top