NEWS

Tuesday, May 24, 2011

To get the postback control id in ASP.Net

Public Shared Function GetPostBackControlName(ByVal page As Page) As String
Dim control As Control = Nothing
'* * First we will check the "__EVENTTARGET" because if the postback is made * by controls which used the _doPostBack function, it will be available in the Request.Form collection.
Dim ctrlname As String = page.Request.Params("__EVENTTARGET")
If Not [String].IsNullOrEmpty(ctrlname) Then
control = page.FindControl(ctrlname)
Else
'* * If __EVENTTARGER is null, the control is a button-type and * need to iterate over the form collection to find it.
Dim c As Control = Nothing
Dim ctrlStr As String = Nothing
For Each ctl As String In page.Request.Form
If ctl.EndsWith(".x") OrElse ctl.EndsWith(".y") Then
'* * ImageButtons have an additional "quasi-property" in their ID which identifies * the mouse-coordinates (X and Y).
ctrlStr = ctl.Substring(0, ctl.Length - 2)
c = page.FindControl(ctrlStr)
Else
c = page.FindControl(ctl)
End If
If TypeOf c Is Button OrElse TypeOf c Is ImageButton Then
control = c
Exit For
End If
Next
End If
If control IsNot Nothing Then
Return control.ID
End If
Return String.Empty
End Function

No comments:

Post a Comment