Click here to Skip to main content
Click here to Skip to main content
Go to top

Post-back Targeting

, 9 May 2012
Rate this:
Please Sign up or sign in to vote.
Overwriting the __doPostBack() function to target the post-back form according to eventTarget

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 LinkButtons?

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 ImageButtons or Buttons 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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Ali Al Omairi(Abu AlHassan)
Software Developer (Senior) The first Ones
Jordan Jordan
-- Life is Good.
Follow on   Twitter

Comments and Discussions

 
SuggestionMulti-Line Strings in VB.NET PinprotectorAspDotNetDev15-May-12 22:36 
GeneralRe: Multi-Line Strings in VB.NET PinmemberAli Al Omairi(Abu AlHassan)16-May-12 3:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140916.1 | Last Updated 9 May 2012
Article Copyright 2012 by Ali Al Omairi(Abu AlHassan)
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid