473,396 Members | 2,002 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Trying to pass a variable back from a user control

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

Nov 19 '05 #1
12 2900
Phil:
Check out: http://openmymind.net/communication/index.html

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/
"Phil Certain" <c9******@fastmail.co.uk> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
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

Nov 19 '05 #2
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.

Nov 19 '05 #3
Phil,
Can you zip up that sample solution and send it to ka********@hotmail.com ?
I'll take a look at it and let you know, can't see anything immediatly
wrong...

Cheers,
Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/
"Phil Certain" <c9******@fastmail.co.uk> wrote in message
news:11**********************@c13g2000cwb.googlegr oups.com...
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.

Nov 19 '05 #4
Thanks Karl, I've emailed the files.

Cheers

Phil

Nov 19 '05 #5
Phil,
I haven't gotten anything yet..is ur email: c9******@fastmail.co.uk ?

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

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/
"Phil Certain" <c9******@fastmail.co.uk> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Thanks Karl, I've emailed the files.

Cheers

Phil

Nov 19 '05 #6
Hi Karl, yes the email is c9groups followed by fastmail.co.uk...thanks
again, Phil

Nov 19 '05 #7
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>

Nov 19 '05 #8
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

"Phil Certain" <c9******@fastmail.co.uk> wrote in message
news:11**********************@c13g2000cwb.googlegr oups.com...
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>

Nov 19 '05 #9

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

--
mbodar
-----------------------------------------------------------------------
mbodart's Profile: http://www.msusenet.com/member.php?userid=5
View this thread: http://www.msusenet.com/t-89579

Nov 19 '05 #10
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

--
MY ASP.Net tutorials
http://www.openmymind.net/
"mbodart" <mb************@no-mx.msusenet.com> wrote in message
news:mb************@no-mx.msusenet.com...

I am having the exact same issue. I was wondering if you had found a
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 from
the aspx page, however, I cannot declare and use the control from the
aspx.vb page.
--
mbodart
------------------------------------------------------------------------
mbodart's Profile: http://www.msusenet.com/member.php?userid=54
View this thread: http://www.msusenet.com/t-895792

Nov 19 '05 #11
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!

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

Phil

Nov 19 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Frank Rizzo | last post by:
I've got a number of user controls on the web page. How can I pass some data to it? I don't see where the user control is instantiated in the page code-behind page. Thanks.
8
by: darrel | last post by:
I'm still trying to fully understand how best to pass variables between pages/usercontrols/each other. On a current site I've done, I've had one userControl do the logic and set the variable,...
5
by: Nathan Sokalski | last post by:
I have a user control that contains three variables which are accessed through public properties. They are declared immediately below the "Web Form Designer Generated Code" section. Every time an...
5
by: Learner | last post by:
Hello, Here is the code snippet I got strucked at. I am unable to convert the below line of code to its equavalent vb.net code. could some one please help me with this? static public...
4
by: simon | last post by:
hello. relatively new to vb.net, i'm using VS 2003 and .net 2.0 i have a web app that i'm i have a user control that displays a simple 1 row table as the header of the page. the user control...
6
by: AppleBag | last post by:
I'm having the worst time trying to login to myspace through code. Can someone tell me how to do this? Please try it yourself before replying, only because I have asked this a couple of times in...
6
by: DMUM via AccessMonster.com | last post by:
Hello I am trying to pass the name of my subform to a function/sub but I can't seem to get it to work. I am using an autokey function (ctrl E) to unlock text boxes on a subform. I have a few...
3
by: =?Utf-8?B?UmljaCBIdXRjaGlucw==?= | last post by:
I have a form, Form1, that shows an instance of Form2 when a user clicks a button. On Form2, the user is to select items, Customer Names for example, from a datagrid control. When the user clicks a...
7
by: Rob | last post by:
lets assume I want to remove a control from a form that was dynamically created... If the button was added at design time, I can do the following... me.Controls.Remove(Button4) How may I...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.