Saturday, March 14, 2009

Get the post back control

You can handle all the events in Page_PreRender like this:

Private Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender

If GetPostBackControl(Me.Page) IsNot Nothing AndAlso GetPostBackControl(Me.Page).UniqueID = ddlFolder.UniqueID Then

'<<Handle Folder DropDownList Post Back>>

'...

ElseIf GetPostBackControl(Me.Page) IsNot Nothing AndAlso GetPostBackControl(Me.Page).UniqueID = btnEdit.UniqueID Then

'<<Handle Edit Button Post Back>>

'...

End If

End Sub

Public Shared Function GetPostBackControl(ByVal page As System.Web.UI.Page) As System.Web.UI.Control

Dim control As Control = Nothing

Dim ctrlname As String = page.Request.Params("__EVENTTARGET")

If ctrlname IsNot Nothing AndAlso ctrlname <> [String].Empty Then

control = page.FindControl(ctrlname)

Else

' if __EVENTTARGET is null, the control is a button type and we need to

' iterate over the form collection to find it

Dim ctrlStr As String = [String].Empty

Dim c As Control = Nothing

For Each ctl As String In page.Request.Form

' handle ImageButton controls ...

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

Exit For

End If

Next

End If

Return control

End Function


Notes extracted from Peter's article:

  • Most ASP.NET Controls are "Server Controls". This means that when an action occurs, the Page, which hosts all its controls inside the FORM, makes a regular old HTTP POST, with itself being the target URL. This is referred to as a "POSTBACK".
  • For non-button server controls, ASP.Net provides the javascript __doPostBack() function to be called by the onclick or onchange event of the control. This function records the name of the object that fired the event, as well as any additional event information, places it into the hidden form fields __EVENTTARGET and __EVENTARGUMENT, and then submits the form, initiating the PostBack.
  • Button types
    • Button type controls DO NOT run the "__doPostBack" script method
    • Button types don't populate __EVENTTARGET
    • All they actually do is SUBMIT the form.
  • On the server side, any controls that implement either the IPostBackDataHandler or IPostBackEventHandler interfaces will examine the PostBack data(__EVENTTARGET, __EVENTARGUMENT) and see if they have raised the client-side event. If so, the corresponding server-side event is raised and the event handler method is called.

The __doPostBack function:

<input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" />

<input type="hidden" name="__EVENTARGUMENT" id="__EVENTARGUMENT" value="" />

function __doPostBack(eventTarget, eventArgument) {

if (!theForm.onsubmit (theForm.onsubmit() != false)) {

theForm.__EVENTTARGET.value = eventTarget;

theForm.__EVENTARGUMENT.value = eventArgument;

theForm.submit();

}

}


References:

ASP.NET: Which Control Posted Back?

Understanding the JavaScript __doPostBack Function

blog comments powered by Disqus