私が抱えている問題の説明は少し複雑ですので、ご容赦ください。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
2番目のボタンをクリックすると問題が発生します。手順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