我有一個包含usercontrol(ascx)的頁面(aspx)。在aspx的回發中,我想閱讀ascx中的一些控制值。因此,在閱讀good articles後,我在我的aspx中創建了一個事件,在回發時觸發。事件處理程序位於ascx中,只是將我感興趣的值寫入視圖狀態,以便父頁面可以讀取它們。 問題是我的事件處理程序永遠不會被擊中,即使我看到事件在我逐步執行代碼時引發。所以,我的代碼炸彈試圖讀取視圖狀態(這是空的)用戶控件中的自定義事件
這裏的時候,就是我在我的aspx得:
Partial Public Class Visitor
Inherits System.Web.UI.Page
Public Event PagePostback(ByVal sender As Object, ByVal e As System.EventArgs)
.
.
.
Private Sub SaveAssignment(ByVal e As EventArgs)
RaiseEvent PagePostback(Me, e) 'Tell usercontrol to post its control values to the viewstate
'read viewstate and save its values
End Sub
下面是在我的ascx:
Partial Public Class GenVstr
Inherits System.Web.UI.UserControl
Protected WithEvents pageVisitor As Visitor
Public Property visitType() As String
Get
Return ViewState("visitType").ToString
End Get
Set(ByVal value As String)
ViewState("visitType") = value
End Set
End Property
Public Sub ParentPostback(ByVal sender As Object, ByVal e As EventArgs) Handles pageVisitor.PagePostback
With Me
If optVIP.Checked Then .visitType = "VIP"
If optFamily.Checked Then .visitType = "Family"
If optMedia.Checked Then .visitType = "Media"
If optGuest.Checked Then .visitType = "Guest"
If optConference.Checked Then .visitType = "Conference"
End With
End Sub
哦,我碰巧提到我正在動態加載ascx? :)