用户控件中的自定义事件

发布于 2024-11-06 18:50:16 字数 1550 浏览 1 评论 0原文

我有一个包含用户控件(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

苄①跕圉湢 2024-11-13 18:50:16

我想我发现了问题。它在这一行中:

Public Sub ParentPostback(ByVal sender As Object, ByVal e As EventArgs) Handles pageVisitor.PagePostback

具体来说,在我的 Handles 部分中,“pageVisitor”是我声明的 WithEvents 变量的名称。我不应该处理这个问题,而应该处理我的用户控件的名称(其 ID)PagePostback 事件。

所以它应该看起来像这样:

Public Sub ParentPostback(ByVal sender As Object, ByVal e As EventArgs) Handles [myusercontrol's ID].PagePostback

I think I found the problem. It was in this line:

Public Sub ParentPostback(ByVal sender As Object, ByVal e As EventArgs) Handles pageVisitor.PagePostback

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:

Public Sub ParentPostback(ByVal sender As Object, ByVal e As EventArgs) Handles [myusercontrol's ID].PagePostback
氛圍 2024-11-13 18:50:16

我认为您需要在创建动态创建控件时调用 addhandler 。

I think you need to call addhandler on the dynamically create control when it is created.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文