“对象引用未设置为对象的实例”的问题 [英] problem with "Object reference not set to an instance of an object"

查看:68
本文介绍了“对象引用未设置为对象的实例”的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我试图创建一个类,必须将主页中的

< linktag的可见改为用户登录时''false''。但是我得到

错误:对象引用未设置为对象的实例

为行''如果mpg.FindControl(" lkred") .Visible = True然后''。


我无法找到解决方案。

任何帮助都将不胜感激...

谢谢

克里斯


班级:

---------

进口Microsoft.VisualBasic

公共类loginkl

Public Sub logkl()

Dim pg As New Page

Dim mpg As MasterPage

如果pg.User.Identity.IsAuthenticated = True那么

如果mpg.FindControl(" lkred")。Visible = True Then

....

结束如果

结束次级

结束班级


代码隐藏:

-----------

部分类MasterPage

继承System.Web。 UI.MasterPage

受保护的子Page_Init(ByVal发送者为对象,ByVal e As

System.EventArgs)处理Me.Init
Dim lg As new loginkl

lg.logkl()

End Sub

End Class


masterpage.master:

------------------

< link runat = "服务器" ID = QUOT; lkred" HREF =" App_Themes文件/ red.css" rel =样式表

type =" text / css" visible =" true" />

Hi,

I tried to create a class which must change the propety ''visible'' of a
<linktag in the masterpage into ''false'' when the user is logged. But i get
the error: "Object reference not set to an instance of an object"
for the line ''If mpg.FindControl("lkred").Visible = True Then''.

I couldn''t find sofar the solution.
Any help would be appreciated ...
Thanks
Chris

the class:
---------
Imports Microsoft.VisualBasic
Public Class loginkl
Public Sub logkl()
Dim pg As New Page
Dim mpg As MasterPage
If pg.User.Identity.IsAuthenticated = True Then
If mpg.FindControl("lkred").Visible = True Then
....
End If
End Sub
End Class

code-behind:
-----------
Partial Class MasterPage
Inherits System.Web.UI.MasterPage
Protected Sub Page_Init(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Init
Dim lg As New loginkl
lg.logkl()
End Sub
End Class

masterpage.master:
------------------
<link runat="server" id="lkred" href="App_Themes/red.css" rel=Stylesheet
type="text/css" visible="true"/>

推荐答案

Chris写道:
Chris wrote:




我试图创建一个类,必须将主页中的

< linktag的可见改为用户登录时''false''。但是我得到

错误:对象引用未设置为对象的实例

为行''如果mpg.FindControl(" lkred") .Visible = True然后''。


我无法找到解决方案。

任何帮助都将不胜感激...

谢谢

克里斯


班级:

---------

进口Microsoft.VisualBasic

公共类loginkl

Public Sub logkl()

Dim pg As New Page
Hi,

I tried to create a class which must change the propety ''visible'' of a
<linktag in the masterpage into ''false'' when the user is logged. But i get
the error: "Object reference not set to an instance of an object"
for the line ''If mpg.FindControl("lkred").Visible = True Then''.

I couldn''t find sofar the solution.
Any help would be appreciated ...
Thanks
Chris

the class:
---------
Imports Microsoft.VisualBasic
Public Class loginkl
Public Sub logkl()
Dim pg As New Page



在这里,您将创建一个全新的Page类实例。

Page类是页面的基类,并且根本不包含任何控件


Here you are creating a completely new instance of the Page class. The
Page class is the base class for pages and doesn''t contain any controls
at all.


Dim mpg As MasterPage

如果pg.User.Identity.IsAuthenticated = True则

如果mpg.FindControl(" lkred")。Visible = True则
Dim mpg As MasterPage
If pg.User.Identity.IsAuthenticated = True Then
If mpg.FindControl("lkred").Visible = True Then



FindControl方法返回一个空引用。由于页面对象中没有

控件,因此无法找到您要查找的控件


The FindControl method returns a null reference. As there are no
controls in the page object, the control you are looking for can of
course not be found.


....

结束如果

结束次级

结束班级


代码隐藏:

-----------

部分类MasterPage

继承System.Web.UI。 MasterPage

受保护的子Page_Init(ByVal发送者作为对象,ByVal e As

System.EventArgs)处理Me.Init

Dim lg作为新loginkl

lg.logkl()
....
End If
End Sub
End Class

code-behind:
-----------
Partial Class MasterPage
Inherits System.Web.UI.MasterPage
Protected Sub Page_Init(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Init
Dim lg As New loginkl
lg.logkl()



这里你应该将对当前页面的引用传递给方法,

,以便它可以使用它来定位控件。


更好的是,为什么不将对控件的引用传递给方法。

那样你不必使用FindControl来定位它,并且方法

可以用来隐藏你喜欢的任何控件,而不仅仅是一个控制器我命名

" lkred" ;.


我不确定,但母版页的Init事件也可能发生

太早访问页面的控件。如果尚未解析页面的标记

,则页面尚未包含任何控件。

然后你必须使用在循环后期发生的偶数。

Here you should to pass a reference to the current Page into the method,
so that it can use that to locate the control.

Better yet, why not pass a reference to the control into the method.
That way you don''t have to use FindControl to locate it, and the method
can be used to hide any control that you like, not only a control named
"lkred".

I am not sure, but the Init event of the master page might also occur
too early to access the controls of the page. If the markup of the page
has not yet been parsed, the page does not yet contain any controls.
Then you have to use an even that occurs later in the cycle.


End Sub

结束班


masterpage.master:

------------------

< link runat =" server" ID = QUOT; lkred" HREF =" App_Themes文件/ red.css" rel =样式表

type =" text / css" visible =" true" />

End Sub
End Class

masterpage.master:
------------------
<link runat="server" id="lkred" href="App_Themes/red.css" rel=Stylesheet
type="text/css" visible="true"/>



-

G?ran Andersson

_____
http://www.guffa.com


更正:我现在看到您在MasterPage

对象中查找控件,而不是在Page对象中。


同样适用,但是实际的错误发生是因为你有

刚刚声明了对母版页的引用,并且没有分配任何值

到它。当你尝试使用引用时,它是空的。


(你确定这是你正在使用的实际代码吗?我会

期望编译器出错,因为你试图使用一个没有给b $ b分配任何值的变量。)


你必须发送一个参考进入方法,可以是对

实际母版页的引用,也可以是对控件的引用。

母版页的基类不能用于访问任何特定母版页中的控件。


-

G ?跑安德森

_____
http://www.guffa。 com
Correction: I see now that you look for the control in the MasterPage
object, not in the Page object.

The same appplies, though, but the actual error occurs because you have
just declared a reference to a master page, and haven''t assign any value
to it. When you try to use the reference, it''s null.

(Are you sure that this is the actual code that you are using? I would
expect a compiler error as you are trying to use a variable that hasn''t
been assigned any value.)

You have to send a reference into the method, either a reference to the
actual master page, or a reference to the control. The base class for
master pages can not be used to access controls in any specific master page.

--
G?ran Andersson
_____
http://www.guffa.com


谢谢


我在课堂上更改了这一行:

Dim mpg作为新的MasterPage


我已经没有错误了,但是它没有将主题从红色改为

green,而是保持红色。所以至少有一个测试仍然是假的:

如果pg.User.Identity.IsAuthenticated = True则

如果mpg.FindControl(" lkred")。可见=真则


你能不能给我一些正确的代码来做你告诉我的事情?

同样适用,但实际的错误是因为你有

刚刚声明了对母版页的引用,并且没有为它分配任何值







您必须向方法发送一个引用,或者是对

实际母版页的引用,或者是对控件的引用

再次感谢

Hi, thanks

I changed this line in the class:
Dim mpg As New MasterPage

I have no error anymore, but instead of changing the theme from red into
green, it remains red. So at least one test remains false:
If pg.User.Identity.IsAuthenticated = True Then
If mpg.FindControl("lkred").Visible = True Then

Could you please give me the right code for doing what you told me?
The same appplies, though, but the actual error occurs because you have
just declared a reference to a master page, and haven''t assign any value
to it.

and

You have to send a reference into the method, either a reference to the
actual master page, or a reference to the control

thanks again


这篇关于“对象引用未设置为对象的实例”的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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