Trying to pass a variable back from a user control

P

Phil Certain

Hi,

I'm trying to do something very simple...or at least it should be. I
have created a host page (gen.aspx) and a very simple user control
(us.ascx). The corresponding code-behind files are gen.aspx.vb and
uc.ascx.vb. With simple html or self contained vb in the user control,
everything is fine and dandy. So the next stage is to pass back a
simple variable from the user control to the host page. I used VS.NEt
to create the files.

The code-behind files are:

gen.aspx.vb:

Imports System
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports Microsoft.VisualBasic
Imports System.Web.UI.UserControl
Imports testing 'see below

Public Class gen
Inherits System.Web.UI.Page

Protected rightnav2 As New incRightNav2

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

End Class



uc.ascx.vb:

Imports System
Imports System.Web.UI.WebControls
Imports System.Configuration.ConfigurationSettings

Namespace testing

Public Class incRightNav2
Inherits System.Web.UI.UserControl

Public strMainCSS

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

Public Sub switchCSS(ByVal sCSS As Object, ByVal e As
EventArgs)

If sCSS.text = "larger" Then
strMainCSS = AppSettings("altCSS")
btn_switchCSS.Text = "smaller"
Label1.Text = strMainCSS
Else
strMainCSS = AppSettings("mainCSS")
btn_switchCSS.Text = "larger"
Label1.Text = strMainCSS

End If
Label1.Text = strMainCSS
End Sub

End Class
End Namespace


incRightNav2 is the id of the user control when instantiated in
gen.aspx.


All I want to do at this point is pass the variable 'strMainCSS' back
to the calling page, but not even getting that far! I get one of two
errors when calling gen.aspx via the browser. With 'Namespace testing'
included in us.ascx.vb, I get:

Compiler Error Message: BC30466: Namespace or type 'testing' for the
Imports 'testing' cannot be found.

Source Error:

Line 15: Imports System.Web.UI.UserControl
Line 16:
Line 17: Imports testing
Line 18:
Line 19:

And if I remove the 'Namespace testing' wrapper, I always get:


Compiler Error Message: BC30002: Type 'incRightNav2' is not defined.

Source Error:

Line 62: Public rightnav2 As New incRightNav2
Line 63:
Line 64:


In each case, VS.Net 'intellisense' finds the class incRightNav2 and no
design time errors are thrown.

So it seems that it's falling over due to some 'type' issue. Its
logical that gen.aspx.vb needs to know about uc.ascx.vb, but I just
can't find a way to get the host page code-behind to reference the user
control code-behind.

I hope its something really basic that I'm doing wrong here...any help
really appreciated.

Phil
 
P

Phil Certain

Hi Karl,

Thanks for your reply, I've read the excellent article you mentioned
and some of the others on your site. I've tried several things as
suggested by the article but it's still falling over on something
fundamental - and most likely staring me right in the eyes. In
desperation I've created the simplest page and user control that I
could muster - and yes I still get the same error i.e. :

BC30002: Type 'myUserControl' is not defined.

All I want to do is (stop tearing my hair out!! and) return a variable
from myUserControl for use on SamplePage - the ultimate aim being to
set a style sheet URL on the main page according to a selection made in
the user control.

I just cannot seem to instantiate myUserControl on SamplePage.aspx
without getting the BC30002 error above.

The code-behinds are:

SamplePage.aspx.vb:

Imports System
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports Microsoft.VisualBasic
Imports System.Web.UI.UserControl

Public Class SamplePage
Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "
..
..
#End Region


Private myUserControl1 As myUserControl ' FALL OVER HERE

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

'-- Dim testUserControl As myUserControl =
CType(Page.LoadControl("myUserControl.ascx"), UserControl)
'--Dim testUserControl = Page.LoadControl("myUserControl.ascx")

Dim myPageVariable = myUserControl1.myUCVariable()

End Sub

End Class

myUserControl.ascx.vb

Imports System.Web.UI.WebControls

Public Class myUserControl
Inherits System.Web.UI.UserControl

Protected WithEvents _myVariable As Literal


Public Property myUCVariable() As String
Get
Return _myVariable.Text
End Get
Set(ByVal Value As String)
_myVariable.Text = Value
End Set
End Property


#Region " Web Form Designer Generated Code "
..
..
#End Region


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
Label2.Text = "User Control loaded"

End Sub

End Class

myUserControl1 is the ID of the user control in SamplePage.aspx.

Simple enough...have experimented until the cows came home with calling
the user control programmatically with Page.LoadControl but always
BC30002...arrghh!
Any help or further pointers gratefully received.

Thanks in advance.
 
K

Karl Seguin

Phil,
Can you zip up that sample solution and send it to (e-mail address removed) ?
I'll take a look at it and let you know, can't see anything immediatly
wrong...

Cheers,
Karl
 
K

Karl Seguin

Phil,
I haven't gotten anything yet..is ur email: (e-mail address removed) ?

if so I'll email you from my private account so you can reply with the
attachment...

Karl
 
P

Phil Certain

Hi Karl, yes the email is c9groups followed by fastmail.co.uk...thanks
again, Phil
 
P

Phil Certain

Hi again...have now made some progress on this. I recreated the project
in VS to clear out some sort of corruption and VS now creates webform
and usercontrol pages wth the correct directives. The compiler runs
correctly too, meaning that I can use 'codebehind=' rather than 'src='.
(Thanks for your earlier help, Karl)

The BC30002 error has now gone!...but...I'm still having a problem
trying to return a variable from the usercontrol (myUserControl.ascx)
to the main page (SamplePage.aspx). I've simplified the code as much as
possible and have included it below.

When I try to access myUCVariable using:

lblMessage.Text = myUserControl1.myUCVariable

I get the following runtime message:

Exception Details: System.NullReferenceException: Object reference not
set to an instance of an object

at the line above. I've tried several variants but always get the same
error. Have even used a button_click event on the main page to try and
retrieve the value of myUCVariable.

Again, all I want to do is return the value of a variable generated
within the user control for use on the main page. It seems that the way
to do this is to set up a property with the control and access this on
the main page. Is there a simpler way?

So either I'm viewing this as something simpler than it is and am not
doing enough to retrieve the variable/property or I'm doing something
basically wrong.

Any insight or suggestions gratefully received...

Phil

SamplePage.aspx.vb
------------------------------

Imports System
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports Microsoft.VisualBasic
Imports System.Web.UI.UserControl
Imports System.ComponentModel
Imports vstest2

Public Class SamplePage
Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "
..
..
#End Region

Public myUserControl1 As New vstest2.myUserControl


Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Label1.Text = "testing 123"

lblMessage.Text = myUserControl1.myUCVariable

End Sub


End Class


myUserControl.ascx.vb
---------------------------------

Imports System.Web.UI.WebControls


Public Class myUserControl
Inherits System.Web.UI.UserControl

Public Property myUCVariable() As String
Get
Return _myVariable.Text
End Get
Set(ByVal Value As String)
_myVariable.Text = Value
End Set
End Property

#Region " Web Form Designer Generated Code "
..
..
#End Region


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

'Some programming here to assign a value to _myVariable
_myVariable.Text = "A TestValue"

End Sub

End Class


myUSerControl is instanced on the main page with:

<uc1:myUserControl id="UserControl1"
runat="server"></uc1:myUserControl>
 
M

mbodart

I am having the exact same issue. I was wondering if you had found
solution?

Basically, my issue is that I have a user control and a web form page
each with a code behind file. I can Register and use the control fro
the aspx page, however, I cannot declare and use the control from th
aspx.vb page
 
W

William F. Robertson, Jr.

You user control is defined on your page
<uc1:myUserControl id="UserControl1" runat="server" />

That means there should be a protected or public field variable on your page
called UserControl1. You have your field defined as

Public myUserControl1 As New vstest2.myUserControl

There is no control on your .aspx page that has an ID of myUserControl1.
Try changing the name of the variable to "UserControl1" or atleast get them
the same in the codebehind as the .aspx page.

Also if that fails to work, you could always find the control you self.

myUserControl1 = CType( Page.FindControl( "UserControl1" ),
vstest2.myUserControl )

Note the parameter passed to FindControl is the ID of the control as defined
on the .aspx side of the page.

HTH,

bill
 
K

Karl Seguin

MBodart:
Phil's problem was simply with a matter of using a mix of src= and
Inheritsattributes in his usercontrols and pages, thus creating odd
namespace conflicts check your <%@ Page and <%@ Control directives to make
sure you aren't doing anything inconsistent...if you are using vb.net you
should just see CodeBehind and inherits...no src.

If everything seems alright, there really ought not be a problem. A user
control or a page is a class like any other class. The difference comes
from how you create an instance. normally you use "new CLASSNAME" but with
user controls you use Page.LoadControl OR you place it in the html with a
runat="server". Either way, you declare them as dim x as
myUserControlClassName just like you would any other class. Like other
classes, namespaces play a key role.

If you provide a simple example of what error you are getting, I'm sure we
can work it through.

Karl
 
P

Phil Certain

Thanks for the reply William - changing the field definition to:

Public UserControl1 As New vstest2.myUserControl
did the trick. Brilliant! ....happy days are here again!
 
P

Phil Certain

Hi mbodart - I followed Karl's advice and finally cracked it with a
later suggestion fro William, see my later posting.

Phil
 

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,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top