ASP.NET: Find which control caused a postback
From Wiki
Summary: Pass the page to the function and the control that caused the postback will be returned from the function
- Public Function GetPostBackControl(ByVal page As System.Web.UI.Page) As System.Web.UI.Control
- ' Find which control caused the postback
- Dim control As Control = Nothing
- Dim ctrlname As String = page.Request.Params("__EVENTTARGET")
- If Not (ctrlname Is Nothing) AndAlso Not (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
- If ctl.EndsWith(".x") OrElse ctl.EndsWith(".y") Then
- ctrlStr = ctl.Substring(0, ctl.Length - 2)
- c = page.FindControl(ctrlStr)
- Else
- c = page.FindControl(ctl)
- End If
- If TypeOf c Is System.Web.UI.WebControls.Button OrElse TypeOf c Is System.Web.UI.WebControls.ImageButton Then
- control = c
- ' break
- End If
- Next
- End If
- Return control
- End Function
This Hack is part of the ASP.NET Hacks collection


