【问题标题】:GridView inside UserControl RowCreated event is firing even after the UserControl is not visible anymore即使在 UserControl 不再可见之后,UserControl RowCreated 事件中的 GridView 也会触发
【发布时间】:2016-04-29 07:42:58
【问题描述】:

这是场景,

我有一个包含 3 个用户控件的菜单的页面,当时只有一个 UC 可见。 每个 UC 都有自己的 GridView,以及各自的 RowCreated 事件。 我为每个 UC 的 RowCreated 事件设置了 3 个断点。

从页面上,当我访问其中一个 UC 时,比如说 UC_A,只有它的 RowCreated 事件正在触发,这很好。但是当我访问另一个 UC UC_B 时,UC_A 的 RowCreated 事件再次触发,之后 UC_B 的 RowCreated 事件是发射这是预期的行为..

在 UC 之间的切换之间执行的唯一代码是在父页面中:

    Protected Sub dlMenu_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs)
        UC_A.Visible = false
        UC_B.Visible= true
        UC_B.LoadPrincipalPage() 'Which has no code in common with the UC_A
    End Sub

请注意,在父页面的 dlMenu_ItemCommand 之前触发了 RowCreated 事件。 为什么会这样?

【问题讨论】:

    标签: asp.net vb.net gridview user-controls


    【解决方案1】:

    您应该在 Page_Load 事件中设置可见性。现在,您将在页面生命周期的后期更改可见性,此时最初可见的控件已触发其事件。

    如果你正在做一个常规的回发,那么使用下面的方法。

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
    Dim postbackControl As String = Page.Request.Params("__EVENTTARGET")
    
    If Not String.IsNullOrWhiteSpace(postbackControl) AndAlso postbackControl = dlMenu.UniqueID Then
        UC_A.Visible = False
        UC_B.Visible = True
    End If
    
    End Sub
    

    你甚至可以把这段代码放在各个用户控件的Page_Load事件中,根据回发控件使用户控件不可见。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-22
      • 1970-01-01
      相关资源
      最近更新 更多