0

我有一些处理计时器方法的代码,它只是更新标签并将其更改为此时的最后更新。

Protected Sub specialNotesTimer_Tick(ByVal sender As Object, ByVal e As EventArgs) Handles specialNotesTimer.Tick
    Label1.Text = "Panel refreshed at: " + DateTime.Now.ToLongTimeString()
End Sub

但是,当它滴答作响时,无法通过按下按钮找到页面上的任何其他控件。缺少图像按钮或我的其他动态创建的控件,我们就是这种情况。我有一个函数可以在页面上找到触发的控件,然后返回控件,以便我可以确定要做什么

Public Shared Function GetPostBackControl(ByVal thePage As Page) As Control
    Dim myControl As Control = Nothing
    Dim ctrlName As String = thePage.Request.Params.Get("__EVENTTARGET")
    If ((ctrlName IsNot Nothing) And (ctrlName <> String.Empty)) Then
        myControl = thePage.FindControl(ctrlName)
    Else
        For Each Item As String In thePage.Request.Form
            Dim c As Control = thePage.FindControl(Item)
            If (TypeOf (c) Is System.Web.UI.WebControls.Button) Then
                 myControl = c
            End If
        Next

    End If
    Return myControl
End Function

这在 timer.tick 之前有效,但在之后无效。该控件不会在 Page.Request.Form 中列为项目,并且当我知道该控件存在时,它会抛出一个空异常。

4

0 回答 0