尝试从用户控件传回变量 [英] Trying to pass a variable back from a user control

查看:62
本文介绍了尝试从用户控件传回变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我正在尝试做一些非常简单的事情......或者至少应该这样做。我已经创建了一个主页(gen.aspx)和一个非常简单的用户控件

(us.ascx)。相应的代码隐藏文件是gen.aspx.vb和

uc.ascx.vb.在用户控件中使用简单的html或自包含的vb,

一切都很好,花花公子。所以下一步是将一个

简单变量从用户控件传回主页。我使用VS.NEt

来创建文件。


代码隐藏文件是:


gen .aspx.vb:


进口系统

进口System.Web.UI

进口System.Web.UI.WebControls

进口Microsoft.VisualBasic

进口System.Web.UI.UserControl

进口测试''见下文


公共类gen

继承System.Web.UI.Page


受保护的rightnav2为新的incRightNav2


Private Sub Page_Load(ByVal sender As System.Object,ByVal e As

System.EventArgs)处理MyBase.Load

''将用户代码置于此处初始化页面< br $>

结束次级


结束班级


uc.ascx.vb:


进口系统

进口System.Web.UI.WebControls

进口System.Configuration.ConfigurationSettings


命名空间测试


公共类incRightNav2

我nherits System.Web.UI.UserControl


Public strMainCSS


Private Sub Page_Load(ByVal sender As System.Object,ByVal e As

System.EventArgs)处理MyBase.Load

End Sub


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

EventArgs)


如果sCSS.text =" large"然后

strMainCSS = AppSettings(" altCSS")

btn_switchCSS.Text =" small"

Label1.Text = strMainCSS
Else

strMainCSS = AppSettings(" mainCSS")

btn_switchCSS.Text =" large"

Label1.Text = strMainCSS


结束如果

Label1.Text = strMainCSS

结束子


结束类

结束命名空间

incRightNav2是在

gen.aspx中实例化时用户控件的id。

此时我想做的就是将变量''strMainCSS''返回

传递到调用页面,但是甚至没有那么远!通过浏览器调用gen.aspx时,我收到了两个

错误之一。通过'。命名空间测试''

包含在us.ascx.vb中,我得到:


编译器错误消息:BC30466:命名空间或类型''测试''对于

无法找到进口''测试''。


来源错误:


Line 15:进口System.Web.UI.UserControl

第16行:

第17行:进口测试

第18行:

第19行:


如果我删除''命名空间测试''包装器,我总是得到:

编译器错误消息:BC30002:类型''incRightNav2''未定义。


来源错误:


第62行:公共rightnav2为新的incRightNav2

第63行:

第64行:

在每种情况下,VS.Net''intellisense''找到类incRightNav2而不是

设计时间错误被抛出。


因此,由于某些类型问题,它似乎正在崩溃。 gen.aspx.vb需要知道的关于uc.ascx.vb的逻辑是什么
,但我只是

找不到获取主页代码的方法-behind引用用户

控制代码隐藏。


我希望它的东西非常基本我在这里做错了...任何帮助

非常感谢。


菲尔

解决方案

Phil :

退房: http://openmymind.net/ communication / index.html


Karl


-

我的ASP.Net教程
http://www.openmymind.net/

Phil Certain < C9 ****** @ fastmail.co.uk>在消息中写道

news:11 ********************** @ z14g2000cwz.googlegr oups.com ...



我正在尝试做一些非常简单的事情......或者至少应该这样做。我创建了一个主页(gen.aspx)和一个非常简单的用户控件
(us.ascx)。相应的代码隐藏文件是gen.aspx.vb和
uc.ascx.vb.在用户控件中使用简单的html或自包含的vb,
一切都很好,花花公子。所以下一步是将一个简单的变量从用户控件传回主页。我使用VS.NEt
来创建文件。

代码隐藏文件是:

gen.aspx.vb:
进口系统
进口System.Web.UI
进口System.Web.UI.WebControls
进口Microsoft.VisualBasic
进口System.Web.UI.UserControl
进口测试''见下面

Public Class gen
继承System.Web.UI.Page

受保护的rightnav2为新的incRightNav2

私有Sub Page_Load(ByVal sender As System.Object,ByVal e As
System.EventArgs)处理MyBase.Load
''用户代码初始化页面

End Sub <结束班

uc.ascx.vb:

进口系统
进口System.Web.UI.WebControls
进口System.Configuration.ConfigurationSettings

命名空间测试

公共类incRightNav2
继承System.Web.UI.UserControl

公共strMainCSS
System.EventArgs)处理MyBase.Load
End Sub


公共子交换机CSS(ByVal sCSS作为对象,ByVal e As
EventArgs)
如果sCSS.text =更大的话然后
strMainCSS = AppSettings(" altCSS")
btn_switchCSS.Text =" less"
Label1.Text = strMainCSS
另外
strMainCSS = AppSettings(" mainCSS")
btn_switchCSS.Text =" large"
Label1.Text = strMainCSS

结束如果
Label1.Text = strMainCSS
End Sub <结束类
结束命名空间

incRightNav2是在
gen.aspx中实例化时用户控件的id。

我现在要做的就是将变量''strMainCSS''传回给调用页面,但是甚至没有那么远!通过浏览器调用gen.aspx时出现两个
错误之一。通过us.ascx.vb中包含的命名空间测试,我得到:

编译器错误消息:BC30466:命名空间或类型''测试'为
无法找到进口''测试'。

来源错误:

第15行:导入System.Web.UI.UserControl
第16行:第17行:进口测试
第18行:
第19行:

如果我删除''命名空间测试''包装器,我总是得到:

编译器错误消息:BC30002:类型''incRightNav2''未定义。

源错误:

第62行:公共rightnav2为新的incRightNav2
第63行:
第64行:

在每种情况下,VS.Netintellisense发现类incRightNav2并且没有设计时间错误。

因此,由于某些类型问题,它似乎正在崩溃。它的逻辑是gen.aspx.vb需要了解的uc.ascx.vb,但我只是找不到一种方法来获取主机页面代码隐藏以引用用户
我希望它的东西非常基本,我在这里做错了...任何帮助
非常感谢。

菲尔



嗨Karl,


感谢您的回复,我读过你提到的优秀文章

以及您网站上的其他一些人。我已经尝试了几篇文章,但文章中提到了b
,但是它仍然会在某些事情上挣扎,而且很可能在我眼中盯着我。在

绝望中,我已经创建了最简单的页面和用户控件,我可以鼓励他们 - 是的,我仍然得到同样的错误,即:


BC30002:类型''myUserControl''未定义。


我想做的就是(停止撕掉我的头发!!)并返回一个变量<来自myUserControl的
用于SamplePage - 最终目的是

根据

中的选择在主页面上设置样式表URL用户控件。


我似乎无法在SamplePage.aspx上实例化myUserControl

而不会出现上面的BC30002错误。


代码隐藏是:


SamplePage.aspx.vb:


进口系统

Imports System.Web.UI

Imports System.Web.UI.WebControls

Imports Microsoft.VisualBasic

Imports System.Web.UI.UserControl


公共类样本页

继承System.Web.UI.Pag e />

#Region" Web表单设计器生成的代码

..

..

#End Region

私有myUserControl1作为myUserControl ''在这里跌落


Private Sub Page_Load(ByVal sender As System.Object,ByVal e As

System.EventArgs)处理MyBase.Load


'' - Dim testUserControl As myUserControl =

CType(Page.LoadControl(" myUserControl.ascx"),UserControl)

''--Dim testUserControl = Page.LoadControl(" myUserControl.ascx")

Dim myPageVariable = myUserControl1.myUCVariable()


结束子


结束班级


myUserControl.ascx.vb


进口System.Web。 UI.WebControls


公共类myUserControl

继承System.Web.UI.UserControl


受保护的事件_myVariable As字面值

公共财产myUCVariable()字符串

获取

返回_myVariable.Tex t $ / $
结束获取

设置(ByVal值为字符串)

_myVariable.Text = Value

结束集

结束财产

#Region" Web表单设计器生成的代码

..

..

#End Region

Private Sub Page_Load( ByVal sender As System.Object,ByVal e As

System.EventArgs)处理MyBase.Load

''将用户代码置于此处初始化页面

Label2.Text =加载用户控件


结束次级


结束班级


myUserControl1是SamplePage.aspx中用户控件的ID。


足够简单......经过实验,直到奶牛回家后才打电话给

用户控制以编程方式使用Page.LoadControl,但总是

BC30002 ... arrghh!

任何帮助或其他指示感激不尽。


提前致谢。


Phil,

您可以压缩该示例解决方案并将其发送到 ka ******** @ hotmail.com

我会看看在它,让你知道,c我什么都看不到

错了...


干杯,

卡尔


-

我的ASP.Net教程
http://www.openmymind.net/

" Phil Certain" < C9 ****** @ fastmail.co.uk>在消息中写道

news:11 ********************** @ c13g2000cwb.googlegr oups.com ...

嗨Karl,

感谢您的回复,我已经阅读了您提到的优秀文章以及您网站上的其他一些文章。我已经尝试了文章中提出的几件事情,但它仍然在基本的东西上摔倒 - 而且很可能正在盯着我看。在绝望中我已经创建了最简单的页面和用户控件,我可以集合 - 是的,我仍然得到相同的错误,即:

BC30002:输入''myUserControl ''没有定义。

我想做的就是(停止撕掉我的头发!!)并从myUserControl返回一个变量
以便在SamplePage上使用 - 最终目标是<根据用户控件中的选择在主页面上设置样式表URL。

我似乎无法在SamplePage.aspx上实例化myUserControl
得到BC30002错误。

代码隐藏是:

SamplePage.aspx.vb:

进口系统
进口系统.Web.UI
Imports System.Web.UI.WebControls
Imports Microsoft.VisualBasic
Imports System.Web.UI.UserControl

公共类SamplePage
继承System.Web.UI.Page

#Region" Web表单设计器生成的代码


#End Region

私人myUserControl1作为myUserControl''在这里落地

私人Sub Page_Load(ByVal sender As System.Object,ByVal e As
System.EventArgs)处理MyBase.Load

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

Dim myPageVariable = myUserControl1.myUCVariable()

End Sub

结束课

myUserControl.ascx.vb

Imports System.Web.UI.WebControls <公共类myUserControl
继承System.Web.UI.UserControl

受保护的WithEvents _myVariable As Literal

公共属性myUCVariable()As String
获取
返回_myVariable.Text
结束获取
设置(ByVal Value as String)
_myVariable.Text = Value
结束集
结束财产

#Region" Web表单设计器生成的代码


#End Region

Private Sub Page_Load(ByVal sender As System.Object,ByVal e As
System.EventArgs)处理MyBase.Load
''用户代码初始化页面
Label2.Text ="加载用户控件

End Sub

结束类

myUserControl1是SamplePage.aspx中用户控件的ID。

足够简单......已经做过实验,直到奶牛回家后才打电话<使用Page.LoadControl以编程方式进行用户控制,但始终是
BC30002 ... arrghh!
感激不尽的任何帮助或其他指示。

提前致谢。


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

解决方案

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



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.


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.



这篇关于尝试从用户控件传回变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆