我遇到的问题有点复杂,所以请耐心等待。我有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