Discussion:
UserControl出現"並未將物件參考設定為物件的執行個體 "訊息
(时间太久无法回复)
Eric
2006-09-11 04:16:01 UTC
Permalink
我寫了一個簡單的UserControl想要測試一下:
Public MustInherit Class WebUserControl
Inherits System.Web.UI.UserControl
Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid

Property DataSource() As DataTable
Get
Return _dt
End Get
Set(ByVal Value As DataTable)
_dt = Value
End Set
End Property

Private _dt As New DataTable()

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Me.DataGrid1.DataSource = _dt
Me.DataBind()
End Sub
End Class

引用的程式如下:
Dim uc_WebUserControl As WebUserControl

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

Private Sub myInit()
Dim dt As New DataTable()
dt = getdatatable()
uc_WebUserControl = Me.FindControl("uc_WebUserControl1")
uc_WebUserControl.DataSource = dt
End Sub

結果出現下列錯誤:

並未將物件參考設定為物件的執行個體
描述: 在執行目前 Web 要求的過程中發生未處理的例外情形。請檢閱堆疊追蹤以取得錯誤的詳細資訊,以及在程式碼中產生的位置。

例外詳細資訊: System.NullReferenceException: 並未將物件參考設定為物件的執行個體

原始程式錯誤(行41):


行 39:
行 40: uc_WebUserControl = Me.FindControl("uc_WebUserControl1")
行 41: uc_WebUserControl.DataSource = dt
行 42:

原本以為dt有問題,
所以先把dt.rows.count秀出來看,
到這裡是沒問題的,
所以應該是UserControl的問題,
只是不曉得那裏出錯了,
請各位先進幫忙,
謝謝!!
Eric
2006-09-11 04:26:01 UTC
Permalink
不好意思,
測到頭昏,
是FindControl的ID給錯了...^^"

"Eric" 來函:
Post by Eric
Public MustInherit Class WebUserControl
Inherits System.Web.UI.UserControl
Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid
Property DataSource() As DataTable
Get
Return _dt
End Get
Set(ByVal Value As DataTable)
_dt = Value
End Set
End Property
Private _dt As New DataTable()
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Me.DataGrid1.DataSource = _dt
Me.DataBind()
End Sub
End Class
Dim uc_WebUserControl As WebUserControl
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
myInit()
End Sub
Private Sub myInit()
Dim dt As New DataTable()
dt = getdatatable()
uc_WebUserControl = Me.FindControl("uc_WebUserControl1")
uc_WebUserControl.DataSource = dt
End Sub
並未將物件參考設定為物件的執行個體
描述: 在執行目前 Web 要求的過程中發生未處理的例外情形。請檢閱堆疊追蹤以取得錯誤的詳細資訊,以及在程式碼中產生的位置。
例外詳細資訊: System.NullReferenceException: 並未將物件參考設定為物件的執行個體
行 40: uc_WebUserControl = Me.FindControl("uc_WebUserControl1")
行 41: uc_WebUserControl.DataSource = dt
原本以為dt有問題,
所以先把dt.rows.count秀出來看,
到這裡是沒問題的,
所以應該是UserControl的問題,
只是不曉得那裏出錯了,
請各位先進幫忙,
謝謝!!
Eric
2006-09-11 06:19:02 UTC
Permalink
請問,
UserControl不能透過property傳DataTable嗎?
我試著把dt傳進去,
再get出來,
結果就變成空的了,
不曉得為什麼?
實在很困擾,
請各為先進幫忙,
感激不盡,
謝謝!!


"Eric" 來函:
Post by Eric
不好意思,
測到頭昏,
是FindControl的ID給錯了...^^"
"Eric" 來函:
Post by Eric
Public MustInherit Class WebUserControl
Inherits System.Web.UI.UserControl
Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid
Property DataSource() As DataTable
Get
Return _dt
End Get
Set(ByVal Value As DataTable)
_dt = Value
End Set
End Property
Private _dt As New DataTable()
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Me.DataGrid1.DataSource = _dt
Me.DataBind()
End Sub
End Class
Dim uc_WebUserControl As WebUserControl
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
myInit()
End Sub
Private Sub myInit()
Dim dt As New DataTable()
dt = getdatatable()
uc_WebUserControl = Me.FindControl("uc_WebUserControl1")
uc_WebUserControl.DataSource = dt
End Sub
並未將物件參考設定為物件的執行個體
描述: 在執行目前 Web 要求的過程中發生未處理的例外情形。請檢閱堆疊追蹤以取得錯誤的詳細資訊,以及在程式碼中產生的位置。
例外詳細資訊: System.NullReferenceException: 並未將物件參考設定為物件的執行個體
行 40: uc_WebUserControl = Me.FindControl("uc_WebUserControl1")
行 41: uc_WebUserControl.DataSource = dt
原本以為dt有問題,
所以先把dt.rows.count秀出來看,
到這裡是沒問題的,
所以應該是UserControl的問題,
只是不曉得那裏出錯了,
請各位先進幫忙,
謝謝!!
ASP.NET MVP - §õ«¶¿«
2006-09-29 05:13:59 UTC
Permalink
œÐ°Ý¡A
UserControl€£¯à³z¹Lproperty¶ÇDataTable¶Ü?
§ÚžÕµÛ§âdt¶Ç¶i¥h¡A
ŠAget¥XšÓ¡A
µ²ªGŽNÅÜŠšªÅªº€F¡A
€£Ÿå±o¬°€°»ò?
¹êŠb«Ü§xÂZ¡A
œÐŠU¬°¥ý¶iÀ°Š£¡A
·P¿E€£ºÉ¡A
ÁÂÁÂ!!
"Eric" šÓšç¡G
€£Šn·N«ä¡A
ŽúšìÀY©ü¡A
¬OFindControlªºIDµ¹¿ù€F...^^"
"Eric" šÓšç¡G
Post by Eric
Public MustInherit Class WebUserControl
Inherits System.Web.UI.UserControl
Protected WithEvents DataGrid1 As
System.Web.UI.WebControls.DataGrid
Property DataSource() As DataTable
Get
Return _dt
End Get
Set(ByVal Value As DataTable)
_dt = Value
End Set
End Property
Private _dt As New DataTable()
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Me.DataGrid1.DataSource = _dt
Me.DataBind()
End Sub
End Class
Dim uc_WebUserControl As WebUserControl
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
myInit()
End Sub
Private Sub myInit()
Dim dt As New DataTable()
dt = getdatatable()
uc_WebUserControl = Me.FindControl("uc_WebUserControl1")
uc_WebUserControl.DataSource = dt
End Sub
šÃ¥Œ±Nª«¥ó°ÑŠÒ³]©w¬°ª«¥óªº°õŠæ­ÓÅé
Žy­z: Šb°õŠæ¥Ø«e Web ­nšDªº¹Lµ{€€µo¥Í¥Œ³B²zªºšÒ¥~±¡§Î¡CœÐÀËŸ\°ïÅ|°lÂÜ¥Hšú±o¿ù»~ªºžÔ²Óžê°T¡A¥H€ÎŠbµ{Š¡œX€€²£¥ÍªºŠìžm¡C
šÒ¥~žÔ²Óžê°T: System.NullReferenceException: šÃ¥Œ±Nª«¥ó°ÑŠÒ³]©w¬°ª«¥óªº°õŠæ­ÓÅé
Šæ 40: uc_WebUserControl = Me.FindControl("uc_WebUserControl1")
Šæ 41: uc_WebUserControl.DataSource = dt
­ì¥»¥H¬°dtŠ³°ÝÃD¡A
©Ò¥H¥ý§âdt.rows.countšq¥XšÓ¬Ý¡A
šì³ožÌ¬OšS°ÝÃDªº¡A
©Ò¥HÀ³žÓ¬OUserControlªº°ÝÃD¡A
¥u¬O€£Ÿå±ošºùØ¥X¿ù€F¡A
œÐŠUŠì¥ý¶iÀ°Š£¡A
ÁÂÁÂ!!
lyman
2006-09-11 09:12:48 UTC
Permalink
Post by Eric
Public MustInherit Class WebUserControl
Inherits System.Web.UI.UserControl
Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid
Property DataSource() As DataTable
Get
Return _dt
End Get
Set(ByVal Value As DataTable)
_dt = Value
End Set
End Property
Private _dt As New DataTable()
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Me.DataGrid1.DataSource = _dt
Me.DataBind()
End Sub
End Class
Dim uc_WebUserControl As WebUserControl
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
myInit()
End Sub
Private Sub myInit()
Dim dt As New DataTable()
dt = getdatatable()
uc_WebUserControl = Me.FindControl("uc_WebUserControl1")
uc_WebUserControl.DataSource = dt
End Sub
KÎŽŒ¢ÎïŒþ…¢¿ŒÔO¶šžéÎïŒþµÄˆÌÐЂ€ów
ÃèÊö: ÔڈÌÐÐÄ¿Ç° Web ÒªÇóµÄß^³ÌÖаlÉúÎŽÌŽÀíµÄÀýÍâÇéÐΡ£Õˆ™z醶ѯB׷ۙÒÔÈ¡µÃåeÕ`µÄԔŒšÙYӍ£¬ÒÔŒ°ÔÚ³ÌÊœŽaÖЮaÉúµÄλÖá£
ÀýÍâԔŒšÙYӍ: System.NullReferenceException: KÎŽŒ¢ÎïŒþ…¢¿ŒÔO¶šžéÎïŒþµÄˆÌÐЂ€ów
ÐÐ 40: uc_WebUserControl = Me.FindControl("uc_WebUserControl1")
ÐÐ 41: uc_WebUserControl.DataSource = dt
Ô­±ŸÒԞédtÓІ–î}£¬
ËùÒÔÏÈ°Ñdt.rows.countÐã³öí¿Ž£¬
ËùÒԑªÔ“ÊÇUserControlµÄ†–î}£¬
Ö»ÊDz»•ÔµÃÄÇÑY³öåeÁË£¬
Ոž÷λÏÈßMŽÍÊ£¬
ÖxÖx!!
继续阅读narkive:
Loading...