Click here to Skip to main content
Click here to Skip to main content

Post-back Targeting

By , 9 May 2012
 

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)

About the Author

Ali Al Omairi(Abu AlHassan)
Software Developer KASHA
Jordan Jordan
Member
-- Life is Good.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
SuggestionMulti-Line Strings in VB.NETprotectorAspDotNetDev15 May '12 - 22:36 
GeneralRe: Multi-Line Strings in VB.NETmemberAli 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   

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130516.1 | Last Updated 9 May 2012
Article Copyright 2012 by Ali Al Omairi(Abu AlHassan)
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid