私が抱えている問題は説明が少し複雑ですので、ご容赦ください。私は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 は実行されません。なぜこれが起こっているのか分かりませんか?
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