Introduction
Sometimes, we need to target the response returning from certain requests to a frame or a window other than the requesting one.
As you might know, to target a GET
request, we add the attribute target
to the HTMLAnchor
or the HyperLink
that causes the request. This is great, but what about post-back requests such as those that are caused by LinkButton
s?
In this tip, I present a way to target post-back responses.
Using the Code
As you might know, every post-back event is handled through the function __doPostBack()
ether directly or through the function WebForm_DoPostBackWithOptions()
. So, to target the post-back response, we need to add the target
attribute of the form. Just like this:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim lines() As String = { _
"var __submissionControl = '';", _
"theForm.onsubmit = function() {", _
" if (__submissionControl != '') {", _
" theForm.target = __getPostBackTarget(__submissionControl);", _
" __submissionControl = '';", _
" }", _
"}", _
"theForm.onclick = function(evt) {", _
" var target = evt.target;", _
" if (target.tagName.toUpperCase() == 'INPUT') {", _
" if (target.type == 'submit' || target.type == 'image')", _
" __submissionControl = target.name;", _
" }", _
" else if (target.tagName.toUpperCase() == 'BUTTON') {", _
" if (target.type == 'submit' )", _
" __submissionControl = target.name;", _
" }", _
"}", _
"function __getPostBackTarget(eventTarget) {", _
" var targetList = [", _
String.Format(" {{ eventTarget: '{0}', _
formTarget: '_blank' }},", cmdBlank.UniqueID), _
String.Format(" {{ eventTarget: '{0}', _
formTarget: '_blank' }},", Button1.UniqueID), _
String.Format(" {{ eventTarget: '{0}', _
formTarget: 'PreviewFrame' }},", cmdIFrame.UniqueID), _
String.Format(" {{ eventTarget: '{0}', _
formTarget: '_self' }}", cmdNoTarget.UniqueID), _
" ];", _
" for (var i = 0; i < targetList.length; i++) {", _
" var target = targetList[i];", _
" if (target.eventTarget == eventTarget) return target.formTarget;", _
" }", _
" return '';", _
"}", _
"function __doPostBack(eventTarget, eventArgument) {", _
" if (!theForm.onsubmit || (theForm.onsubmit() != false)) {", _
" theForm.target = __getPostBackTarget(eventTarget);", _
" theForm.__EVENTTARGET.value = eventTarget;", _
" theForm.__EVENTARGUMENT.value = eventArgument;", _
" theForm.submit();", _
" theForm.target = '';", _
" }", _
"}", _
""}
ClientScript.RegisterStartupScript(Me.GetType(), _
"TargetingPostBack", String.Join(Environment.NewLine, lines), True)
End Sub
The function __doPostBack()
overwrites the existing one by adding the two italic lines; the first sets the target of the form according to the event target (the unique name of the button) and the other returns its value to default just after submission.
And as you can see, the function __getPostBactTarget()
function takes the event target and compares it against a list of event target-form target pairs to return the associated form target. If there is no matching, it returns an empty string
(the default value) instead.
To handle post-backs caused ImageButton
s or Button
s with UseSubmissionBehavior
property set to true
, we handle the two events of the submission form click
and submit
. First on click
, we set the value of __submissionControl
to the UniqueID
of the submission button, then we use it to set the target
of the form on submit
.
History
- Wednesday, May 9, 2012: First posted
- Thursday, May 10, 2012: Added a paragraph describing the function
__getPostBagTarget()
- Sunday, May 27, 2012: Handled submission behavior post-backs