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

Tagged as

Executing a event or function on each postback

, 5 Apr 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
Executing a event or function on each postback

Introduction

Executing a event or function on each postback

Background

Problem: Suppose we want to call an javascript function or an event on each postback eg. show a loading image on each every postback. Using jQuery we can bind a function on form submit. However, it will only work with buttons and input type submit. This will fail when an auto postback occurs or postback occurs through some linkbutton.

Asp.net performs each postback using a javascript function which it inserts in its one and only one form tag. So we will be tweaking this function for our own purpose

Using the Code

  <script type="<a class="attribute-value">text/javascript">
 <![CDATA[
    var theForm = document.forms['form1'];
    if (!theForm) {
      theForm = document.form1;
  }
   function __doPostBack(eventTarget, eventArgument) {
    if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
        theForm.__EVENTTARGET.value = eventTarget;
        theForm.__EVENTARGUMENT.value = eventArgument;
        theForm.submit();
    }
  }
]]>
</script>

Above code is what inserted by ASP.net engine.

 function doSomething() {----};

function __doPostBack(eventTarget, eventArgument) {
    if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
        theForm.__EVENTTARGET.value = eventTarget;
        theForm.__EVENTARGUMENT.value = eventArgument;         
        doSomething();
        theForm.submit();
    }
  }

We will insert our own __doPostBack function at end of form tag so that javascript engine overrides the previous one.

 <form > --code by ASP.net ---   page content---      --- modified script--- </form>

License

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

Share

About the Author

No Biography provided

Comments and Discussions

 
QuestionjQuery: to bind the code to our form Pinmemberericpxz5-Apr-12 5:24 
AnswerRe: jQuery: to bind the code to our form PinmemberLaserson5-Apr-12 19:02 
GeneralRe: jQuery: to bind the code to our form Pinmemberericpxz6-Apr-12 0:05 

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 | Terms of Use | Mobile
Web03 | 2.8.141223.1 | Last Updated 5 Apr 2012
Article Copyright 2012 by v111981@gmail.com
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid