我遇到的问题解释起来有点复杂,所以请多多包涵。我有 2 个按钮控件。在页面加载中,我想知道哪个按钮在页面加载时创建了回发。通过研究,我在下面找到了这个片段,它按预期工作。所以这是我点击按钮时发生的事件场景。
1. Click the button does a postback
2. Runs the function below and tell me the id of the button
3. Runs the clicked event handlers for that button
Protected Sub btnCalc_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnCalc.Click
' Do this
End Sub
当我单击第二个按钮时,问题就出现了,它执行第 1 步和第 2 步,但从不执行第 3 步。通过测试,我发现它在单击第一个按钮时仅执行 1、2 和 3。我不知道为什么会这样?
Function GetPostBackControlName() As String
Dim control As Control = Nothing
Dim ctrlname As String = Page.Request.Params("__EVENTTARGET")
If ctrlname <> Nothing AndAlso ctrlname <> [String].Empty Then
control = Page.FindControl(ctrlname)
Else
Dim ctrlStr As String = [String].Empty
Dim c As Control = Nothing
For Each ctl As String In Page.Request.Form
c = Page.FindControl(ctl)
If TypeOf c Is System.Web.UI.WebControls.Button Then
control = c
Exit For
End If
Next
End If
Try
Return control.ID.ToString
Catch
Return ""
End Try
End Function