用户控件中的自定义事件
我有一个包含用户控件(ascx)的页面(aspx)。在 aspx 回发时,我想读取 ascx 中的一些控制值。因此,在阅读了一些好文章后,我在我的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? :)
I've got a page (aspx) that contains a usercontrol (ascx). On postback of the aspx, I'd like to read some control values in the ascx. So upon reading some good articles, I created an event in my aspx that fires on postback. The event handler is in the ascx, and simply writes the values I'm interested in to the viewstate so that the parent page can read them.
The problem is my event handler never gets hit, even though I see the event is raised as I step through the code. So my code bombs when trying to read the viewstate (which is empty)
Here's what I've got in my 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
Here's what's in my 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
Oh, did I happen to mention I'm dynamically loading the ascx? :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想我发现了问题。它在这一行中:
具体来说,在我的 Handles 部分中,“pageVisitor”是我声明的 WithEvents 变量的名称。我不应该处理这个问题,而应该处理我的用户控件的名称(其 ID)PagePostback 事件。
所以它应该看起来像这样:
I think I found the problem. It was in this line:
Specifically in my Handles section, "pageVisitor" is the name of the variable I declared WithEvents. Instead of handling that, I should have handled the name of my usercontrol (its ID) PagePostback event.
So it should look like this:
我认为您需要在创建动态创建控件时调用 addhandler 。
I think you need to call addhandler on the dynamically create control when it is created.