Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.
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



LTD Social Sitings
Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.