I'm trying to get a reference to the control on my page that causes the postback but I'm running into problems when the postback is caused by the chart (e.g. after the WebChart_ClickSeries event).
I have a user control (.ascx) comprising of a chart control with its autopostback property set to true and several other asp.net controls (e.g. linkbuttons etc.) that also cause a postback. I have a routine in my user control that I use to determine which control causes the postback. Based on the outcome of this I decide whether or not to refresh the chart or not. The GetPostBackControl procedure is as follows:
Code: Select all
Public Shared Function GetPostBackControl(ByVal targPage As Page) As Control
' ASP.NET stores the name of the control that initiated the postback of the WebForm
' in a hidden input field named "__EVENTTARGET" with two exceptions - when Button and
' ImageButton controls are clicked on the form. We need to handle these cases differently
Dim ctrl As Control = Nothing
Dim ctrlname As String = targPage.Request.Params.Get("__EVENTTARGET")
If Not (ctrlname Is Nothing) And ctrlname <> String.Empty Then
ctrl = targPage.FindControl(ctrlname)
Else
' Handle the Button control postbacks
Dim ctl As String
For Each ctl In targPage.Request.Form
Dim c As Control = targPage.FindControl(ctl)
If TypeOf c Is System.Web.UI.WebControls.Button Then
ctrl = c
Exit For
End If
Next ctl
End If
' Handle the ImageButton postbacks
If ctrl Is Nothing Then
' not shown for brevity
End If
Return ctrl
End Function
Code: Select all
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Page.IsPostBack Then
Dim ctrl As Control = GetPostBackControl(Me.Page)
If Not (ctrl Is Nothing) Then
' do something
End If
End If
End Sub
Thanks,
Norman